Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir vs New-Item , is it the same cmdlets?

Tags:

powershell

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?

like image 848
Borys Fursov Avatar asked Jun 13 '18 08:06

Borys Fursov


People also ask

Which is the cmdlet used to create a directory?

New-Item cmdlet is used to create a directory by passing the path using -Path as path of the directory and -ItemType as Directory.

Which of these cmdlets are related to copy operation?

The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it can't copy a file to a certificate drive. This cmdlet doesn't cut or delete the items being copied.

What does mkdir do in PowerShell?

Creates a directory or subdirectory. Command extensions, which are enabled by default, allow you to use a single mkdir command to create intermediate directories in a specified path. This command is the same as the md command.

Which command lists cmdlets related to aliases?

The Get-Alias cmdlet gets the aliases in the current session.


1 Answers

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.

like image 158
Ryan Bemrose Avatar answered Oct 21 '22 10:10

Ryan Bemrose