Scripting Games 2011 commentary I: Line continuations
No I’m not going to give you the answers – you’ll just have to wait until the events close.
During (and possibly after) the games I’m going to comment on some of the things I’ve noticed about PowerShell usage.
I want to start with breaking a PowerShell line across multiple lines. The “one liner” is what many PowerShell users aspire to and we can put together some impressive functionality by stringing together some cmdlets using the pipeline – for instance:
Get-Process | sort CPU -Descending | select -First 6 | Format-Table –AutoSize
Now we’ll assume we need to break this so that it fits on shorter lines. The back tick is the line continuation character so we could end up with this
Get-Process | `
sort CPU -Descending | `
select -First 6 | `
which is actually easier to read. Another alternative could be this
| sort CPU -Descending `
| select -First 6 `
| Format-Table –AutoSize
which again is easy to read. The difference is in the positioning of the back tick before or after the pipe symbol.
Both of these options take more effort than required. All we need to do is this
sort CPU -Descending |
select -First 6 |
The pipeline symbol works as a line continuation symbol as well. Save typing and make life easier.