Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

February 27, 2008 12:00 AM

How to Handle Long PowerShell Statements

Windows IT Pro
InstantDoc ID #97958
Rating: (0)

As a PowerShell statement grows larger, it’s not always practical to enter it on a single line in the PowerShell console window. You can enter a long statement on several lines, but you must take into account how PowerShell treats new lines. When PowerShell determines that a line is incomplete, it continues to the next line when processing the statement. For example, when the first line in a statement ends with the pipe operator, as in

Get-Service |
where {$_.status -eq ‘running’} |
select displayname

PowerShell knows that the statement continues to the next line. This statement returns results similar to those shown in Figure 3. Notice the multiline prompt (>>) that precedes each line after the first line. When PowerShell expects a line to continue to a second line, it uses a multiline prompt for that line. You then type the next line of code at that prompt. Once PowerShell enters this multiline mode, it will continue in this mode and always prompt you with the multiline prompt. When you finish entering the last line, press Enter a second time to execute the command and return to the normal prompt.

Now suppose you break the statement before the pipe operator:

 Get-Service
  | where {$_.status -eq ‘running’}
  | select displayname

PowerShell now interprets the first line as complete and processes it as an entire statement. PowerShell then tries to process the second line, which results in the error message: An empty pipe element is not permitted.

You can remedy this situation by adding a back tick (`) to the end of the lines:

 Get-Service `
  | where {$_.status -eq ‘running’} `
  | select displayname

The back tick tells PowerShell that the statement continues to the next line. The statement now returns the same information shown in Figure 3.

PowerShell processes any line that it thinks is a complete statement. In other words, it automatically terminates a statement when it reaches a new line unless it thinks that the statement continues. However, you can also manually terminate a statement by adding a semi-colon (;) at the end:

 Get-Service |
  where {$_.status -eq ‘running’} |
  select displayname;

This statement returns the same results as those shown in Figure 3.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.