Developer and Text Tools

Tidy PowerShell Scripts: Indentation, Casing, Aliases and Common Mistakes

How to format a PowerShell script safely: brace styles, pipelines, hashtable alignment, cmdlet casing, alias expansion and the checks worth fixing first.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
Tidy PowerShell Scripts: Indentation, Casing, Aliases and Common Mistakes

PowerShell scripts tend to start life in a console. Someone types a pipeline, it works, and it gets pasted into a .ps1 file with aliases, odd casing and no indentation. Months later the script runs every night and nobody wants to touch it. Formatting it is the first step to making it maintainable, and the PowerShell Script Formatter does that in the browser while explaining what it changed.

Why PowerShell needs a careful formatter

In most languages a formatter can freely change spaces between tokens. PowerShell is different because it has two parsing modes. In expression mode, $a -1 and $a-1 can mean different things, and in command mode a space separates arguments. A dot followed by a space runs a script in the current scope, while a dot without a space reads a property. An opening brace on its own line after a cmdlet turns a script block argument into a separate statement.

The formatter therefore changes spacing only where it is safe: around assignment operators, around pipes, after commas, inside braces and after keywords such as if, foreach and switch. Everything else keeps the spacing you wrote. It never moves the brace of a script block passed to a cmdlet, even in Allman mode.

Choosing a brace style

PSScriptAnalyzer ships three formatting presets, and the tool offers the same three. OTBS, the one true brace style, keeps the opening brace on the line of the statement and writes "} else {". Stroustrup is identical except that else, catch and finally start a new line. Allman puts the braces of keywords and function definitions on their own lines. OTBS is the most common choice for scripts shared on GitHub and in the PowerShell Gallery.

Pipelines and continuation lines

A long pipeline is easier to read when each stage sits on its own line. End a line with a pipe character and the next line continues the pipeline; the formatter indents those continuation lines one level so the stages stand out. The same applies to lines that end with a backtick, although trailing pipes are less fragile, because a space after a backtick silently breaks the continuation.

Hashtables

Multi-line hashtables are common in scripts for splatting parameters and building objects. With alignment on, the formatter pads each key so that the = signs line up under the longest key, matching PSScriptAnalyzer's AlignAssignmentStatement rule. Nested hashtables are aligned separately, so one long inner key does not push the outer ones out of line.

Casing and aliases

PowerShell does not care about case, but people reading the script do. The formatter writes about 140 common cmdlets in their official casing, such as Get-ChildItem and ForEach-Object, and keywords and comparison operators in lower case, such as foreach and -eq. Functions defined in the same script are matched too, so a call written in lower case takes the casing of its declaration.

Aliases like gci, ls, % and ? save typing at the console but make scripts harder to read and less portable, because some aliases do not exist on Linux and macOS. Each alias is reported with its full cmdlet name. Turn on Expand aliases and they are replaced, but only in command position, at the start of a statement or after a pipe, so the word select inside an argument is left alone.

The checks that find real bugs

Some warnings are about style, others prevent bugs. The one most worth fixing is a comparison with $null on the right, such as $result -eq $null. When $result is an array, PowerShell filters the array instead of testing it, and the condition can be true or false for the wrong reasons. Put $null on the left.

Function names that use a verb outside the approved list are flagged, because Import-Module warns about them and they make commands harder to discover. Write-Host is noted because its output cannot be captured, and Invoke-Expression because it runs strings as code.

Here-strings and comments

Here-strings delimited by @" and "@, comment blocks between <# and #>, and ordinary strings are copied exactly as written. Since the closing marker of a here-string must start at the beginning of a line, the formatter never indents it.

Making it part of the routine

Format a script before you commit it, read the diff, and fix the warnings that remain. When you work on a whole module, open every .ps1, .psm1 and .psd1 file at once, apply the formatting to all of them, and download a ZIP. Nothing is executed during any of this, and the script never leaves your computer.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages