PowerShell command-line interface provides different commands in order to create a directory or folder. The New-Item command is the most popular and native way to create a directory with different attributes. Also, the md and mkdir commands can be used to create directories even they are not built-in commands provided by PowerShell.
Create Directory with New-Item Command
The New-Item command or cmdlet is provided to create new items like directories, files, folders, links, etc. The New-Item can be used to create a directory by specifying the -ItemType directory attribute and providing the name or path of the new directory.
New-Item -ItemType directory newfolder
Alternatively, the absolute path or full path can be provided into the New-Item command where given new directory in the given path will be created. Also using double quotes is more reliable while providing paths and folders.
New-Item -ItemType directory "C:\data\newfolder"
Also, the New-Item provides the -Path attribute where the path and folder name can be specified in a more structured way.
New-Item -ItemType directory -Path "C:\data\newfolder"
The New-Item can be also used to create relative path and folders. In the following example we will create a directory named “newfolder” in the parent directory.
New-Item -ItemType directory -Path "..\newfolder"
Alternatively, the directory name and path can be specified separately by using the -Path and -Name attributes. The -Path attribute is used to specify the path and -Name is used to specify the name of the folder.
New-Item -ItemType directory -Path "C:\data\" -Name "newfolder"
Create Directory with md Command
The md command is an alias of the New-Item command and uses all of its syntax and attributes. The md command can be used to create a directory by directly providing the folder name. The “md” is the first letter of the words “make directory”.
The output provides the created directory information like the mode of the directory last write time the length, name, etc.
Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 12/16/2020 11:55 PM test
The -Name attribute can be also used to provide the directory name.
md -Name "test"
Create Directory with mkdir Command
The mkdir command is another alias for the New-Item command like the md command. The mkdir can be used the same way as the New-Item and md command. In the following example we will create the directory named “test”.