Idera nSoftware Compellent

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: | ? {$_.name -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: | ? {$_.name -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.


Posted Feb 06 2009, 02:14 AM by Aleksandar
Filed under: , ,

Comments

Tobias Weltner wrote re: Do you know the aliases for the New-Item cmdlet?
on 02-06-2009 10:19 AM

Whenever you are in doubt what a given command really "is", you can always use Get-Command! So

Get-Command mkdir

Get-Command md

will reveal they are functions. In fact, this way you can also detect whether there are "conflicting" commands. For example, when you enter

Get-Command ping

and you have installed PSCX or similar extensions, most likely PowerShell will list more than one result. Whenever there are more commands, PowerShell picks one automatically based on this order: alias, function, cmdlet, external application (I think).

BTW great article....!

Copyright 2010 PowerShell.com. All rights reserved.