Is there a PowerShell code formatter / pretty printer? [closed]
Asked Answered
A

3

49

I'm looking for a source code beautifyer for PowerShell programs. Ideally, it would be CLI based, but any solution is acceptable. I would like to avoid configuring a generic pretty printer tool; I'd like a solution that works for PowerShell out of the box. Is there such a thing?

Alphabetic answered 7/6, 2010 at 5:5 Comment(2)
Try Visual Studio Code with Powershell Extension: marketplace.visualstudio.com/…Fecundate
@Fecundate are you saying that extension has code formatting? It doesn't mention that anywhere?Galina
A
34

UPDATE: it's now on GitHub: https://github.com/DTW-DanWard/PowerShell-Beautifier

I wrote a PowerShell pretty printer / code cleaner in PowerShell. It cleans white space, indents code groups, replaces aliases with commands, fixes casing on commands, parameters, types, etc. You can use it to reformat a file in place or read a source file and output the result in a different file.

More info: http://danspowershellstuff.blogspot.com/2012/03/powershell-pretty-printer-code-cleaner.html

Download page: http://www.dtwconsulting.com/PS/Module_PrettyPrinterV1.htm

Let me know what you'd like to see changed/configurable/etc.

Alcuin answered 3/3, 2012 at 18:25 Comment(6)
YES! You awesome guy you!Derickderide
Have you considered porting your code to GitHub so others can contribute?Goldston
Yep - I've been working on cleaning it up and completing test files and documentation. Should be up in the next few days.Alcuin
It's now on GitHub: https://github.com/DTW-DanWard/PowerShell-BeautifierAlcuin
I cannot get this to work for the life of me. Can you provide better instructions. I downloaded the source now what?Agitprop
Backup/commit your PowerShell file first or practice on a test copy! Assume your file is: c:\Temp\TestFile.ps1 Open PowerShell Window Import-Module C:\Temp\PowerShell-Beautifier\src\DTW.PS.Beautifier.psd1 (Takes a few seconds the first time.) Edit-DTWBeautifyScript c:\Temp\TestFile.ps1 This cleans file in place, overwriting orig copy (thus the need for the backup or commit first). Or you can: Edit-DTWBeautifyScript c:\Temp\TestFile.ps1 c:\Temp\CleanFile.ps1 Outputs the results to a new file, leaving the first untouched. github.com/DTW-DanWard/PowerShell-Beautifier#usageAlcuin
G
3

I don't know of any. However, PowerShell V2 exposes a PowerShell tokenizer you could build on: System.Management.Automation.PsParser

It might be interesting to do some "lint"-like checks, too.

For example, I think scripts should avoid using aliases that aren't marked ReadOnly, as users may have changed them. See Where are the default aliases defined in powershell? for some discussion.

Geologize answered 17/6, 2010 at 17:37 Comment(0)
I
1

Take a look at ScriptCop.
Props to Mike Shepard - I found this from his answer here.

Identity answered 20/10, 2011 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.