Reputation: 579
I found that there are two different cmdlets : New-Item
and mkdir
, firstly I was thinking that mkdir
is one of aliases of New-Item
, but it is not:
Try to get aliases of it, it is md
for mkdir
and ni
for New-Item
:
So I am a little bit confused, what the difference between that cmdlets, because powershell reference gives me almost the same pages: mkdir
, New-Item
But New-Item
is in Microsoft.PowerShell.Management
and mkdir
in Microsoft.PowerShell.Core
, but the do the same(or not?)! Why there are two same cmdlets in powershell?
Upvotes: 24
Views: 16154
Reputation: 9266
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
.
Upvotes: 34