Saturday, March 3, 2012

PowerShell pretty printer / code cleaner V1

PowerShell Beautifier

Now on GitHub:

Formatting Matters

Tabs or spaces; spaces or tabs? If spaces, how many? We sure do take whitespace seriously. But when writing ‘commit-worthy’ PowerShell code, there’s more than just whitespace to think about. Shouldn’t you use cmdlet names instead of aliases? And shouldn’t you have correct casing for cmdlets, methods and types?

PowerShell Beautifier is a PowerShell command-line utility for cleaning and reformatting PowerShell script files, written in PowerShell. Sure, it will change all indentation to tabs or spaces for you - but it will do more than just that. A picture is worth 1KB words; here’s a before/after showing all types of changes including spaces & tabs:

Here's a simpler pic focusing on the alias-replacement and casing changes:

The PowerShell Beautifier makes these changes:
  • properly indents code inside {}, [], () and $() groups
  • replaces aliases with the command names: dir → Get-ChildItem
  • fixes command name casing: get-childitem → Get-ChildItem
  • fixes parameter name casing: Test-Path -path → Test-Path -Path
  • fixes [type] casing
    • changes all PowerShell shortcuts to lower: [STRING] → [string]
    • changes other types (if in memory): [system.exception] → [System.Exception]
  • cleans/rearranges all whitespace within a line