< Back

Edit-DTWBeautifyScript

Sat Jan 18, 2020 9:31 pm

NAME Edit-DTWBeautifyScript



SYNOPSIS

Cleans PowerShell script: re-indents code with spaces or tabs, cleans

and rearranges all whitespace within a line, replaces aliases with

cmdlet names, replaces parameter names with proper casing, fixes case for

[types], etc.





SYNTAX

Edit-DTWBeautifyScript [-SourcePath] <String> [[-DestinationPath] <String>] [[-IndentType] <String>]

[-StandardOutput] [[-NewLine] <String>] [<CommonParameters>]





DESCRIPTION

Cleans PowerShell script: re-indents code with spaces or tabs, cleans

and rearranges all whitespace within a line, replaces aliases with

commands, replaces parameter names with proper casing, fixes case for

[types], etc.



More specifically it:

- 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 shortcut/type accelerators to built-in value ([STRING] -> [string])

changes other types ([system.exception] -> [System.Exception]

only works for types loaded into memory

- cleans/rearranges all whitespace within a line

many rules - see Test-AddSpaceFollowingToken to tweak





----------



IMPORTANT NOTE: this utility rewrites your script in place! Before running this

on your script make sure you back up your script or commit any changes you have

or run this on a copy of your script.



----------



When loading, the module caches all the commands, aliases, etc. in memory

at the time. If you've added new commands to memory since loading the

module, you may want to reload it.





This utility doesn't do everything - it's version 1.

Version 2 (using PowerShell tokenizing/AST functionality) should

allow me to update the parsing functionality. But just so you know,

here's what it doesn't do:

- change location of group openings, say ( or {, from same line to new

line and vice-versa;

- expand param names (Test-Path -Inc -> Test-Path -Include).



See https://github.com/DTW-DanWard/PowerShell-Beautifier or http://dtwconsulting.com

for more information. I hope you enjoy using this utility!

-Dan Ward





PARAMETERS

-SourcePath <String>

Path to the source PowerShell file



Required? true

Position? 1

Default value

Accept pipeline input? true (ByValue, ByPropertyName)

Accept wildcard characters? false



-DestinationPath <String>

Path to write reformatted PowerShell. If not specified rewrites file

in place.



Required? false

Position? 2

Default value

Accept pipeline input? false

Accept wildcard characters? false



-IndentType <String>

Type of indent to use: TwoSpaces, FourSpaces or Tabs



Required? false

Position? 3

Default value TwoSpaces

Accept pipeline input? false

Accept wildcard characters? false



-StandardOutput [<SwitchParameter>]

If specified, cleaned script is only written to stdout, not any file, and

any errors will be written to stderror using concise format (not Write-Error).

This option may be required for integrating with external editors.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-NewLine <String>

If specified, allows user to override line ending type of the host OS. By default

the value of [environment]::NewLine is used for newline, by specifying parameter

NewLine and passing either CRLF or LF, that will be used regardless of host OS.

This is most handy for getting the test script to run on Core on all OSes (all the

test files use CRLF) but could also be useful for beautifying on one platform while

targeting another... or it could just to keep your build manager happy.



Required? false

Position? 4

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS



OUTPUTS



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Edit-DTWBeautifyScript -Source c:\\P\\S1.ps1 -Destination c:\\P\\S1_New.ps1



Gets content from c:\\P\\S1.ps1, cleans and writes to c:\\P\\S1_New.ps1









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Edit-DTWBeautifyScript -SourcePath c:\\P\\S1.ps1



Writes cleaned script results back into c:\\P\\S1.ps1









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>dir c:\\CodeFiles -Include *.ps1,*.psm1 -Recurse -IndentType FourSpaces | Edit-DTWBeautifyScript



For each .ps1 and .psm1 file, cleans and rewrites back into same file using tabs.









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>Edit-DTWBeautifyScript -SourcePath c:\\P\\S1.ps1 -NewLine CRLF



Writes cleaned script results back into c:\\P\\S1.ps1 using Windows-style line endings.











RELATED LINKS