O'Reilly logo

Pro Windows PowerShell by Hristo Deshev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.2. Aliasing Tips, Techniques, and Pitfalls

When it comes to shell scripting, aliases are not, strictly speaking, assets—they can be liabilities too. They shine at saving typing, but they can really hurt readability, because they force the reader to remember what each alias points to. Compare the following snippet:

Get-ChildItem *.txt | Where-Object { $_.Size -gt 5KB} | '
    ForEach-Object { $_.Name }

to this one:

gci *.txt | ? { $_.Size -gt 5KB} | % { $_.Name }

Which one is easier to understand? The first one reads almost like English, while the second may as well be Klingon—even seasoned PowerShell users will have to stop and remember that ? is an alias for Where-Object and % for ForEach-Object. A good rule of thumb that I like to follow is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required