Search PowerShellers and other PowerShell-related sites

Friday, February 6, 2009

Do you know the aliases for the New-Item cmdlet?

I was playing with Doug Finke's extensions for PowerShell ISE (Integrated Scripting Environment), Expand-Alias and Expand-CurrentAlias, when I've noticed that mkdir doesn't expand and md expands to mkdir. Up until now I thought that mkdir and md are aliases for the New-Item cmdlet as rmdir and rd are the aliases for the Remove-Item cmdlet.

In both PowerShell V1 and V2 CTP3:

PS> get-alias | ? {$_.definition -eq 'remove-item'}

CommandType Name  Definition
----------- ----  ----------
Alias       ri    Remove-Item
Alias       rm    Remove-Item
Alias       rmdir Remove-Item
Alias       del   Remove-Item
Alias       erase Remove-Item
Alias       rd    Remove-Item

PS> get-alias | ? {$_.definition -eq 'new-item'}

CommandType Name Definition
----------- ---- ----------
Alias       ni   New-Item

Where are the mkdir and md?

The mkdir and md are built-in functions in PowerShell V1 with the same definition:

PS> gci function: | ? {$ -like 'm*d*'} | ft -auto

CommandType Name  Definition
----------- ----  ----------
Function    mkdir param([string[]]$paths); New-Item -type directory -path $paths
Function    md    param([string[]]$paths); New-Item -type directory -path $paths

The things are different in PowerShell V2 CTP3. The mkdir command is still a function, but with much more complex definition, and md is an alias for mkdir.

PS> gci function: | ? {$ -like 'm*d*'}

CommandType Name  Definition
----------- ----  ----------
Function    mkdir ...

PS> get-alias md

CommandType Name Definition
----------- ---- ----------
Alias       md   mkdir

To see the code behind mkdir command type:

PS> gc function:\mkdir

Very nice example of an advanced function.

No comments: