Monthly Archive

Categories

PowerShell Line continuation

There are a number of ways to signal PowerShell line continuation in your code. probably the most common is to have a pipeline symbol at the end of the line:

Get-Process |
Sort-Object -Property CPU -Descending -Top 5

 

because there’s nothing after the pipe symbol PowerShell assumes that the next line of code is actually a continuation and treats the 2 lines of code as a single pipeline. This is especially good if you have a very long pipeline with incorporating numerous cmdlets.

 

Since the days of PowerShell v1 many people have asked to be able to the pipe symbol at the beginning of the continuation line rather than at the end of the first line. The only way you could do that was to use the backtick ` symbol to signify line continuation:

Get-Process `
| Sort-Object -Property CPU -Descending -Top 5

 

This is an ugly approach as the back tick is a very small character its often overlooked when reading or working with the code which can lead to bugs.

 

PowerShell 7 allows you to use the pipe symbol at the start of a line as a continuation character without needing the backtick:

Get-Process
| Sort-Object -Property CPU -Descending -Top 5

 

Over the years many people, especially those new to PowerShell, have complained that there are sometimes too many different ways in PowerShell to achieve the result you want. To my mind this change of one of those unnecessary changes that just add complexity (of choice) for no real benefit. PowerShell is an open source project and if someone wants to add something like this then the opportunity is there. I’ll be sticking to the pipe symbol at the endo of the line approach as I don’t see any advantage to changing and I find the original approach easier to understand.

Comments are closed.