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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.