Search PowerShellers and other PowerShell-related sites

Tuesday, February 17, 2009

Get-Alias ?

If you want to find the cmdlet "behind" the alias ?, command Get-Alias ? won't help you, because ? will be treated as a wildcard for a single character, and you will get all single-character aliases:

PS>get-alias ?

CommandType  Name    Definition
-----------  ----    ----------
Alias        %       ForEach-Object
Alias        ?       Where-Object
Alias        h       Get-History
Alias        r       Invoke-History

To use ? as the literal value, you have to escape it with a backtick and put it in single quotes:

PS>get-alias '`?' 

CommandType  Name    Definition
-----------  ----    ----------
Alias        ?       Where-Object

Double quotes don't work in both V1 and V2 CTP3. Does somebody know why?

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