Home » PowerShell Tips » PowerShell mkdir to Create Directory

PowerShell mkdir to Create Directory

PowerShell mkdir is a function defined in PowerShell to create a directory and it’s an alias of md command.

powershell mkdir
PowerShell mkdir

PowerShell mkdir uses the New-Item cmdlet to create a directory and has the same syntax as the New-Item cmdlet in PowerShell.

As system administrators, we know a few popular command-line commands like md, mkdir, rmdir, etc.. to work with folders in the Windows operating system. mkdir command is used to create a directory using cmd. It is also available as a function in PowerShell.

In this article, we will discuss how to use the mkdir function in PowerShell to create a folder in the current directory, create a folder in the specified directory, and use the md command to create a directory in Windows.

Let’s understand the mkdir function in PowerShell with examples.

PowerShell mkdir

mkdir function creates an empty folder in Windows. It’s a function in PowerShell.

mkdir Syntax

New-Item [-Path] <string[]> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]
    
New-Item [[-Path] <string[]>] -Name <string> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-UseTransaction] [<CommonParameters>]

The above mkdir syntax uses the New-Item cmdlet to create a directory and has New-Item cmdlet syntax.

mkdir function in PowerShell uses Path, Force, Credentials.. etc parameters to create a directory in the system.

Cool Tip: How to use base64 encode file in PowerShell!

PowerShell mkdir To Create Folder in Current Directory

mkdir function in PowerShell creates a folder in the current directory by default, however, if the path is specified, mkdir creates a folder on the specified path.

For example, run the below command to create a directory in PowerShell.

PS D:\> mkdir LogTest

In the above PowerShell command, mkdir function creates the “LogTest” folder in the current directory and has the output below.

    Directory: D:\


Mode                LastWriteTime         Length Name                                                                       
----                -------------         ------ ----                                                                       
d-----       29-07-2021     05:55                LogTest   

Cool Tip: How to count files in a folder in PowerShell!

PowerShell mkdir -p to Create Directory in Specified Path

The PowerShell mkdir-p creates a directory at the path specified by the -p or -Path parameter.

Let’s consider an example, to create a directory at path “D:\LogTest\FTP-01” using mkdir PowerShell, run the below command.

PS D:\> mkdir -p D:\LogTest\FTP-01

Above mkdir -p command in PowerShell takes the directory path and creates a new directory. Output as below.

    Directory: D:\LogTest


Mode                LastWriteTime         Length Name                                                                       
----                -------------         ------ ----                                                                       
d-----       29-07-2021     05:59                FTP-01 

How to Create a Directory Using PowerShell md Command?

PowerShell md command is used to create a directory in PowerShell. To create a new directory using PowerShell md, run the below command.

PS D:\LogTest> md FTP-02

In the above example, PowerShell md command creates a folder in the current directory in PowerShell.

Cool Tip: How to find large-size files in PowerShell!

Conclusion

PowerShell mkdir function creates a directory in the current directory or using mkdir-p, it creates a directory at the specified path.

mkdir function and PowerShell md command internally use the New-Item cmdlet to create a directory.

Cool Tip: How to use test-connection to ping a list of computers!

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.