Do You Really Want To Use –EQ?

Do You Really Want To Use –EQ?

  • Comments 4

Sebastien posted a cool simple script HERE which effectively does a reverse lookup on aliases. Here is the code:

function Get-AliasShortcut([string]$CommandName) {
ls Alias: | ?{ $_.Definition
-eq $CommandName }
}
Set-Alias gas Get-AliasShortcut

Notice that he used –EQ. Whenever you find yourself using –EQ, ask yourself if that is really want you want. You might be cheating yourself out of a ton great stuff. Consider changing the -EQ to -MATCH

That would allow you to do:

PS> Get-AliasShortcut child

CommandType Name Definition
----------- ---- ----------
Alias gci Get-ChildItem
Alias ls Get-ChildItem
Alias dir Get-ChildItem


PS> Get-AliasShortcut item

CommandType Name Definition
----------- ---- ----------
Alias cli Clear-Item
Alias clp Clear-ItemProperty
Alias cpi Copy-Item
Alias cpp Copy-ItemProperty
Alias gci Get-ChildItem
Alias gi Get-Item
Alias gp Get-ItemProperty
Alias ii Invoke-Item
Alias mi Move-Item
Alias mp Move-ItemProperty
Alias ni New-Item
Alias ri Remove-Item
Alias rni Rename-Item
Alias rnp Rename-ItemProperty
Alias rp Remove-ItemProperty
Alias si Set-Item
Alias sp Set-ItemProperty
Alias cp Copy-Item
Alias ls Get-ChildItem
Alias mv Move-Item
Alias rm Remove-Item
Alias rmdir Remove-Item
Alias copy Copy-Item
Alias del Remove-Item
Alias dir Get-ChildItem
Alias erase Remove-Item
Alias move Move-Item
Alias rd Remove-Item
Alias ren Rename-Item

Sebastien – thanks for the cool script!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 7 and 2 and type the answer here:
  • Post
  • This was almost exactly the first function I made in PowerShell (before even hello-world!).

    Only I called it 'aliases', instead of the much more sensible Verb-Noun + alias. Live and learn, I guess.

  • I vote for "-like". By using like it is up to the caller if he wants wildcard matching or not...

  • "gas child" is a horrible example!

  • > Do You Really Want To Use "gas child"?

    Hmmmmmm.... Not today :-) .  (fixed)

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Page 1 of 1 (4 items)