Powershell Multiline Mode

Source: Internet
Author: User

From: http://www.windowsitpro.com/article/windows-powershell/how-to-handle-long-powershell-statements

Extracted from: http://www.windowsitpro.com/article/windows-powershell/how-to-handle-long-powershell-statements

This article describes how to edit multi-line powershell scripts. One of the problems that plague me is how to end multi-line powershell scripts. Now I find the answer, that is, when you finish editing, press ENTER twice.

The unexpected result is that it can be used ~ The other character of the key) to indicate that powershell still has downstream processing. The original symbol should be an escape character.

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 thisMultiline 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
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.