New-Item
is a cmdlet, defined in an assembly, which creates new objects - both files and directories. mkdir
is a function which calls New-Item
to create directories specifically. It is provided for convenience to shell users who are familiar with Windows CMD or unix shell command mkdir
To see the definition of mkdir
use Get-Content Function:\mkdir
. You can see that it calls New-Item
under the covers, after some parameter and pipeline management. Using PS 5.0:
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('New-Item', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd -Type Directory @PSBoundParameters }
Both of the following commands will create a new directory named foo
in the root of C:\
. The second form is familiar to people coming from other shells (and shorter to type). The first form is idiomatic PowerShell.
PS> New-Item -Path C:\foo -Type Directory
PS> mkdir C:\foo
Because mkdir
hardcodes the -Type Directory
parameter, it can only be used to create directories. There is no equivalent mkfile
built-in function. To create files, use New-Item -Type File
, or another cmdlet such as Out-File
.
Get-Content Function:\mkdir
<# .FORWARDHELPTARGETNAME New-Item .FORWARDHELPCATEGORY Cmdlet #>
- does it means, that it is absolutely the same? If yes, why mkdir is not alias ofNew-Item
? Why Microsoft dublicate code?