20

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: enter image description here

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?

2
  • 4
    To see the definition of mkdir, use Get-Content Function:\mkdir Jun 13, 2018 at 8:05
  • @RyanBemrose <# .FORWARDHELPTARGETNAME New-Item .FORWARDHELPCATEGORY Cmdlet #> - does it means, that it is absolutely the same? If yes, why mkdir is not alias of New-Item ? Why Microsoft dublicate code? Jun 13, 2018 at 8:11

1 Answer 1

30

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.

2
  • thanks, that means I can't create files with mkdir Jun 13, 2018 at 8:17
  • 5
    Alternative to Get-Content Function:\mkdir and faster to type imho: gcm mkdir | fl
    – Clijsters
    Jun 13, 2018 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.