PowerShell for Windows Admins

Apr 8 2011   3:15PM GMT

Scripting Games 2011 commentary I: Line continuations

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


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 | `
Format-Table –AutoSize

which is actually easier to read.  Another alternative could be this

Get-Process `
| 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

Get-Process |
sort CPU -Descending |
select -First 6 |
Format-Table –AutoSize

The pipeline symbol works as a line continuation symbol as well.  Save typing and make life easier.


 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: