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 ...

Get Pro Windows PowerShell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.