PowerShell Basics Level: Beginner Chapter 4: Everyday Commands That Save Hours

Multiple lines in a console

Long commands are easier to read on several lines. Shift plus Enter continues a line, and braces group a block of code together.

Lesson 4 of 4 in Chapter 4: Everyday Commands That Save Hours. Previous step in this path: Formatting, input, and output display. 3 learning objectives, 6 examples, and a knowledge check.

Start: what you will learn

Long commands are easier to read on several lines. Shift plus Enter continues a line, and braces group a block of code together.

Learning objectives

  • Continue a long command over several lines
  • Explain what braces do in a script block
  • Write a readable multiline pipeline

Concept in simple words

What it is: a multiline command is one command that you spread over several lines so it is easier to read. In the console, Shift plus Enter moves you to a new line without running the command. Braces, the curly brackets { and }, group code into a block that belongs together. A multiline command is still one command, it only looks longer on screen.

Why it works

Why you need it: a long one line pipeline is hard to read, hard to check, and hard to fix. When everything sits on one line, your eyes have to scan sideways to find each step, and a mistake in the middle is easy to miss. Splitting the pipeline so each step sits on its own line makes the order visible at a glance. You can check step one, then step two, then step three, in the same order PowerShell will run them.

Why it works this way: PowerShell reads a command until it is complete, and a pipe at the end of a line is a clear signal that more is coming. A script block inside braces is treated as one unit, so a test or a set of steps can be handed to a command as a whole. That is why a pipeline can be split across lines and still run as one command. PowerShell is not guessing where the command ends, it is following clear signals in the code.

Natural line continuation, explained: some characters at the end of a line tell PowerShell that the command is not finished yet. The pipe is the most useful one, because a pipe always needs a next step. A comma also signals continuation, because a list is not finished until the next item appears. An opening bracket, parenthesis, or brace signals continuation, because the matching closing character has not appeared yet. A binary operator such as + or -and at the end of a line also signals continuation, because an operator needs a value on both sides. After any of these, you can press Enter and continue on the next line.

Shift plus Enter, explained: in the console, a plain Enter normally runs the command. If the line you typed already looks complete, PowerShell runs it at once, even if you meant to add more. Shift plus Enter inserts a new line without running the command, so you can keep typing. It is a safety tool for the case where the line would otherwise be complete. When a line already ends with a pipe, you usually do not need Shift plus Enter, because the pipe already tells PowerShell to wait. Learning both tools means you are covered in both cases.

Think of it like this

Think of it like this: a long sentence is easier to read when it is broken into short lines, as long as you can see it is still one sentence. The pipe at the end of a line is like a comma that says, this continues. Braces are like brackets around a paragraph, telling you where that paragraph starts and ends. Indentation is like the margin of that paragraph, it helps your eyes, but it does not change the meaning.

How it works, step by step

How it works, step by step: first, type the first part of your command and end the line with a pipe if another step follows. Next, press Shift plus Enter in the console, or simply press Enter in a script file, and type the next step on its own line. Then indent the lines inside a block by a few spaces, so the block is easy to spot. Finally, close every brace you opened, and only press Enter on its own when the whole command is finished. Read the finished command top to bottom before you run it, because that top to bottom order is the order it will run in.

Braces and script blocks in depth: a script block is code inside { and } that is treated as one unit. Where-Object uses a script block to hold its test, so the test can be passed to Where-Object as a whole. An if statement uses a script block to hold the steps that run when its condition is true. The code inside the braces does not run on its own at the moment you type it, it runs when the command that received it decides to run it, once per item for Where-Object, or once when the condition is true for if. That delay is the point of a block: it packages steps to be run later, under a condition.

Braces are not all the same: PowerShell uses braces for more than one purpose. A script block uses { and }, and a hashtable, which is a set of named pairs, uses @{ and }. They look similar, but they mean different things, so do not swap them. A script block holds steps or a test to run, while a hashtable holds data in named pairs. If a brace block behaves strangely, check whether you wrote { when you needed @{, or the other way round.

Indentation, and what it does not do: indentation means starting a line with a few spaces, so blocks and continued steps stand out. Indentation has no effect on how PowerShell runs the command, except inside a quoted string, where spaces are part of the text. That means indentation alone never continues a command. A continued line still needs a pipe, a comma, an open bracket, or an operator at the end of the previous line. Pretty spacing without a continuation signal is just a new, separate command, which is a common source of confusion.

The backtick, and why to avoid it: the backtick character, written as `, can continue a line where no natural continuation signal exists, but it must be the very last character on the line, with no space after it. A trailing space you cannot see breaks it, and a backtick in the middle of a line is easy to miss when reading. For those reasons, the backtick is fragile and is best avoided in scripts. Prefer a pipe, a comma, or an open bracket to split a line, and if a command has many parameters, consider a cleaner layout rather than forcing a backtick split.

When PowerShell waits for more input: if you open a brace, a quote, or a parenthesis and do not close it, PowerShell shows a continuation prompt, often written as >>, and waits for the rest. That prompt is not an error, it is PowerShell saying the command is not complete yet. Type the missing closing character to finish, or press Ctrl plus C to cancel the unfinished input and start again. Ctrl plus C cancels what you are typing, it does not undo a command that has already run, so use it calmly when you see the continuation prompt and realise the command went wrong.

Splitting a command in a script file: in a script file, the same continuation rules apply, but you do not need Shift plus Enter, because a plain Enter in a file does not run anything until the whole script runs. That makes script files a comfortable place to practise multiline layout. Write one pipeline step per line, indent blocks, and keep the closing brace on its own line, so the structure is visible. A script that reads well is easier to review, and a script that is easy to review is easier to trust.

Let us walk through an example: in a pipeline that starts Get-Service, then Where-Object on the next line, then Select-Object on the next, each line is one step. The pipes at the ends of the first lines tell PowerShell to keep reading. The braces around the Where-Object test hold that test together. Look at the result and check that it matches the test, and that the line breaks did not change the meaning. If the result is wrong, check the test first, not the line breaks, because line breaks alone do not change what a pipeline does.

When to use it

When to use it, and when not to: use several lines whenever a command gets long, or when you are building a pipeline you want to check step by step. It is especially helpful in scripts, where a reader will thank you later. Do not split a command in the middle of a word or a quoted string, and do not press plain Enter in the console in the middle of a command, because that runs it early. Do not split a line just for the sake of it either, because a short command on one line is already readable, and splitting it can make it harder, not easier, to follow.

Worked examples

Read each example in order. Each one explains its parts, then shows what to look for in the output.

Syntax

Syntax
Get-Command |
  Where-Object { <test> }

Example 1

One pipeline step per line. The pipe at the end of a line tells PowerShell the command continues. Where-Object keeps processes whose CPU value is greater than 10, with $_ meaning the current process. Select-Object keeps only Name and CPU for display. Look for only processes above the threshold, in a short readable list.

Example 1
Get-Process |
  Where-Object { $_.CPU -gt 10 } |
  Select-Object Name, CPU

Illustrative output: Name CPU ---- --- pwsh 12.5

Example 2

Braces group the block that runs when the condition is true. Indentation makes the block easy to see. The condition $true is always true, so the block always runs in this teaching example, and Write-Output sends Yes onward. The closing brace on its own line marks the end of the block clearly.

Example 2
if ($true) {
  Write-Output "Yes"
}

Illustrative output: Yes

Example 3

Get-Service gets service objects. The pipe at the end of the first line tells PowerShell the command continues. Where-Object keeps only services whose Status equals Running, with $_ meaning the current service. Sort-Object Name orders the kept services by name. Select-Object keeps only Name and Status for display, on the final line.

Example 3
Get-Service |
  Where-Object { $_.Status -eq "Running" } |
  Sort-Object Name |
  Select-Object Name, Status

Illustrative output: Name Status ---- ------ Spooler Running

Example 4

The comma after Spooler signals that the name list is not finished, so the command continues on the next line. BITS is the second name in the list, and the pipe then sends both service objects to Select-Object. Look for both services in the result, and check that neither name was cut off by the line break.

Example 4
Get-Service -Name Spooler,
  BITS | Select-Object Name, Status

Illustrative output: Name Status ---- ------ Spooler Running

Example 5

This is a multiline pipeline with a combined test. The first pipe continues to Where-Object, whose braces hold a test with two parts joined by -and, so both must be true. The second pipe continues to Select-Object, which keeps only Name and Status. Look for only running services whose names start with S. The line breaks change nothing about the test, they only make it readable.

Example 5
Get-Service |
  Where-Object { $_.Status -eq "Running" -and $_.Name -like "S*" } |
  Select-Object Name, Status

Illustrative output: Name Status ---- ------ Spooler Running

Example 6

The + operator at the end of the first line signals continuation, because an operator needs a value on both sides. PowerShell therefore waits for the second line, adds 10 and 20, and stores 30 in $total. Write-Output then sends the stored value onward. Look for 30, which confirms that the two lines were treated as one addition, not as two separate commands.

Example 6
$total = 10 +
  20
Write-Output $total

Illustrative output: 30

Nothing on this page executes PowerShell code. The outputs above are illustrative only.

Common mistakes and troubleshooting

Read these before you try the examples, so you can spot the same problems in your own work.

  • Pressing Enter in the middle of a pipeline and wondering why it ran early. Use Shift plus Enter, or end the line with a pipe.
  • Forgetting a closing brace, which leaves PowerShell waiting for more input. Count opening and closing braces.
  • Indenting a continued line but forgetting the pipe at the end of the previous line, so PowerShell runs the first line on its own and the rest looks like a separate, confusing command.
  • Splitting a command in the middle of a word or inside a quoted string. A line break inside a word changes the word, and a line break inside a quoted string can change the text, so only split at a pipe, a comma, an operator, or an open bracket.
  • Putting a space after a backtick used for line continuation. The backtick must be the very last character on the line, so a trailing space silently breaks the continuation. Prefer a pipe or another natural continuation instead.
  • Seeing the continuation prompt and typing more commands as if it were a fresh prompt. When PowerShell shows that it is waiting, first finish or cancel the unfinished command, with the closing character or with Ctrl plus C, before you start a new one.
  • Assuming indentation alone continues a command. Indentation only helps reading, it does not tell PowerShell to wait, so a line without a continuation signal still runs on its own.

Knowledge check

Knowledge check

How do you continue a command on a new line in the console without running it?

Pick one answer. Feedback appears right away, and you can try again.

Remember this and next step

Remember this: pipe at the end means more to come, braces mark a block, and indentation helps your eyes. A command that reads well is a command you can check, and a command you can check is one you can trust. If PowerShell waits at a continuation prompt, look for an unclosed brace or quote before you type anything else.

Next step: Scripts and security: execution policy