Welcome to MSDN Blogs Sign in | Join | Help

Do You Really Want To Use –EQ?

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

Published Thursday, June 28, 2007 5:35 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Do You Really Want To Use –EQ?

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.

Thursday, June 28, 2007 5:25 AM by Simon Buchan

# re: Do You Really Want To Use –EQ?

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

Thursday, June 28, 2007 7:04 AM by Maximilian Haenel

# re: Do You Really Want To Use "gas child"?

"gas child" is a horrible example!

Sunday, July 01, 2007 7:38 PM by Neville

# re: Do You Really Want To Use –EQ?

> 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

Monday, July 02, 2007 10:38 AM by PowerShellTeam

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker