To write a PowerShell multiline command, end each line with something that can’t finish a statement, like a pipe |, a comma or an opening bracket. PowerShell then reads the next line as part of the same command:
Get-ChildItem -Path .\reports |
Where-Object Length -gt 1KB |
Sort-Object -Property Name
For a long list of parameters, put them in a hashtable and splat it. The backtick works too, but it breaks easily.
In this tutorial, I’ll walk through each way to split a long PowerShell command, the mistakes that bite and what changes between PowerShell 7 and Windows PowerShell 5.1.
Which line-break method should you use?
Here’s the short version. I tested every row in PowerShell 7.6 and Windows PowerShell 5.1:
| Method | Use it for | Works in 5.1? |
|---|---|---|
Line ends with |, ,, an operator or ( { [ | Pipelines, arrays, math, conditions | Yes |
| Splatting a hashtable or array | Commands with many parameters | Yes |
Backtick ` at the end of the line | Quick fixes when nothing else fits | Yes |
Line starts with | | Pipelines read top to bottom | No (PowerShell 7+) |
Semicolon ; | Several short commands on one line | Yes |
The first row covers most scripts. New to the pipeline? Start with PowerShell pipeline variables.
Break lines naturally after a pipe, comma or bracket
When a line ends with a pipe, PowerShell knows the pipeline isn’t finished and keeps reading. This sample filters orders for a team in Austin, TX:
$orders = @(
[pscustomobject]@{ Customer = 'Maria Lopez'; City = 'Austin'; Total = 1250 }
[pscustomobject]@{ Customer = 'Tom Baker'; City = 'Denver'; Total = 480 }
[pscustomobject]@{ Customer = 'Dana Reyes'; City = 'Seattle'; Total = 2100 }
)
$orders |
Where-Object { $_.Total -gt 1000 } |
Sort-Object -Property Total -Descending |
ForEach-Object { "$($_.Customer) in $($_.City): `$$($_.Total)" }
Output:
Dana Reyes in Seattle: $2100
Maria Lopez in Austin: $1250
The @( works the same way: the array stays open until its closing parenthesis.
Leading spaces don’t matter, so indent for readers. The same rule lets you spread a Where-Object filter over several lines.
Binary operators and commas also keep a statement open. Put the + or the comma at the end of the line, never at the start of the next one:
$subtotal = 1250 +
480 +
2100
$cities = 'Austin',
'Denver',
'Seattle'
"Subtotal: `$$subtotal across $($cities.Count) cities"
Output:
Subtotal: $3830 across 3 cities
Use the backtick for line continuation
The backtick (the grave accent key next to 1 on a US keyboard) is PowerShell’s escape character. At the very end of a line, it escapes the line break, so the command goes on:
function Send-Report {
param($To = 'nobody', $City = 'none', $Format = 'PDF')
"$Format report for $City to $To"
}
Send-Report `
-To 'Maria Lopez' `
-City 'Austin' `
-Format 'CSV'
Output:
CSV report for Austin to Maria Lopez
It works in both versions, but Microsoft’s about_Parsing page says to avoid it: one invisible character breaks it.
Why does a space after the backtick break the command?
The backtick only continues the line when the line break comes right after it. In this file there’s one space after the backtick on the Send-Report line:
function Send-Report {
param($To = 'nobody', $City = 'none', $Format = 'PDF')
"$Format report for $City to $To"
}
Send-Report `
-To &