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?


$hay@Israel said...

Good point, that's why I use the long way in my scripts:

PS > $alias = "?"
PS > Get-Alias | Where-Object {$ -eq $alias}

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

That way I don't need to worry about escaping.

aleksandar said...

I've just found out that if you want to use double quotes, you have to escape it twice:

PS>Get-Alias "``?"

Is that a bug?

Jason Archer said...

@aleksandar, not a bug, just how parsing works. In this case you have two different things parsing your command. You have the command line evaluation, and you have Get-Alias evaluating the expression you give it.

Since ? is a special character for Get-Alias, you have to escape it. But, ` is a special character to the command parser. So you either have to escape the escape sequence (``? which becomes `?) or you have to turn parsing off by using single quotes ('`?').

aleksandar said...

Jason, thank you for an excellent explanation. As a matter of fact, we don't have to use quotes at all:

PS>get-alias ``?