PowerShell Basics Level: Beginner Chapter 3: Think in Objects, Not Text

Other command chaining

Besides the pipe, PowerShell can run commands one after another, or run the next command only when the first one succeeds or fails.

Lesson 4 of 7 in Chapter 3: Think in Objects, Not Text. Previous step in this path: The pipeline and parameter binding. 3 learning objectives, 6 examples, and a knowledge check.

Start: what you will learn

Besides the pipe, PowerShell can run commands one after another, or run the next command only when the first one succeeds or fails.

Learning objectives

  • Run commands in sequence with a semicolon
  • Use the success and failure operators correctly
  • Choose chaining over a long pipeline when steps are independent

Concept in simple words

What it is: Command chaining means running more than one command in a single line, either in order no matter what happens, or only when the previous command succeeds or fails. Chaining is about order and conditions, not about data, and that distinction is the whole lesson.

Why it works

Why it exists: Sometimes you want to run two independent steps in one line, such as showing the date and then showing one service, without writing a script file. Sometimes you want a second step only when the first step worked, or a fallback step only when the first step failed. Chaining gives you those three patterns in one line, with a semicolon for order, && for success, and || for failure.

Why it works this way: Chaining controls order and conditions, but it passes nothing between commands. That is the key difference from the pipeline, which passes objects. A semicolon says run this next, while && says run this next only if the last command succeeded, and || says run this next only if the last command failed. The second command in a chain starts fresh, with no pipeline input from the first command, even though variables set by the first command are still available, because variables live in the shared session.

Think of it like this

Think of it like this: A semicolon is like a to do list, do this, then do that, whatever happens. The && operator is like saying, if the first errand worked, do the second one too. The || operator is like saying, if the first errand failed, try this backup plan instead. In all three cases, the second errand does not receive a bag of results from the first errand, it only knows whether the first errand is done, or whether it succeeded or failed.

How it works, step by step

How it works, step by step: First, ask whether the second command needs the output of the first. If it does, do not use chaining, use a pipeline or a variable instead, because chaining passes no data. If the steps are independent, join them with a semicolon, for example Get-Date; Get-Service -Name Spooler. The first command runs, then the second command runs, and the outputs appear in order.

How it works, step by step, continued: Next, if the second step depends on success or failure, use && or ||, but check your version first. These two operators work in PowerShell 7, and they are not available in Windows PowerShell 5.1, so the same line can work on one edition and fail on the other. In Windows PowerShell 5.1, PowerShell reports that && is not a valid statement separator, which is its way of saying that operator does not exist in that edition.

What success means for && and ||, in detail: Success here does not mean a true or false value returned by the command. It means the pipeline finished without failure, as tracked by the automatic variable $?, which holds true if the last pipeline succeeded and false if it failed. PowerShell also uses $LASTEXITCODE for external programs, but for the cmdlets in this chapter, $? is the idea to hold on to. A command can return the value False and still count as succeeded, because it ran correctly and its answer simply happened to be False.

Why it works

Why that success rule matters: This is the most important correction in this lesson. A test that returns False is not the same as a failed command. For example, a lookup that correctly answers False, meaning no match was found, still succeeded as a command, so && after it would still run. If you need to act on a true or false answer, do not use && or ||, use an if statement, which tests the value itself, not whether the command succeeded.

How to test a true or false value correctly: Use if, with the test in parentheses, such as if (Test-Connection -ComputerName localhost -Count 1 -Quiet) { Write-Output "Reachable" }. Here Test-Connection sends one ping to localhost, which is your own computer, and -Quiet returns true or false instead of a long report. The if statement reads that true or false value, and runs the first block only when the value is true. Add an else block when you want a different message for the false case, such as else { Write-Output "Not reachable" }.

How && works, step by step: PowerShell runs the left hand pipeline first. If $?, the success flag, is true after that pipeline, PowerShell runs the right hand pipeline. If $? is false, PowerShell skips the right hand pipeline. So && is a gate that opens only on success, and it does not pass the left hand output to the right hand side. The right hand side must get its own input, from its own arguments or from a variable.

How || works, step by step: PowerShell runs the left hand pipeline first. If $? is false after that pipeline, PowerShell runs the right hand pipeline as a fallback. If $? is true, PowerShell skips the right hand pipeline, because there is nothing to fall back from. So || is a gate that opens only on failure, and like &&, it passes no data to the right hand side.

How precedence works, in simple words: Precedence is the order in which PowerShell groups parts of a line. The pipe groups before && and ||, so in a line like Get-Service | Select-Object -First 1 && Write-Output "Done", the pipe part is treated as the left hand pipeline, and && checks whether that whole piped step succeeded. The semicolon groups after && and ||, so a semicolon separates whole chained groups. You do not need to memorise a full precedence table, but when a chained line confuses you, add parentheses around the piped part or split the line with semicolons to make the grouping visible.

What a terminating error does to a chain: A terminating error is a hard error that stops execution, and pipeline chain operators do not absorb it. So if the left hand side throws a terminating error, the whole chain can stop, and || does not reliably act as a catch all for that kind of error. Treat || as a fallback for ordinary failure, signalled by $?, not as a safety net for every possible error, and keep error handling in a script simple and explicit when the stakes are higher.

Let us walk through an example: In Test-Connection -ComputerName localhost -Count 1 -Quiet && Write-Output "Test finished", Test-Connection tests a connection to localhost, which is your own computer, -Count 1 sends one test, and -Quiet returns true or false instead of a long report. The && then runs Write-Output only if the Test-Connection pipeline succeeded, which it normally does when it completes without error. Note the message says Test finished, not Reachable, because && checks success, not the true or false answer, and Reachable would need an if statement instead.

Let us walk through a second example: In Get-Item -Path "C:\NoSuchFolder\NoSuchFile.txt" -ErrorAction SilentlyContinue || Write-Output "Lookup failed", Get-Item tries to find a file that does not exist, and -ErrorAction SilentlyContinue hides the error message but does not change the failure flag. The || then runs Write-Output, because the lookup pipeline failed, so the message Lookup failed appears. Change the path to a file that does exist, and || skips the message, because the lookup succeeded.

When to use it

When to use it, and when not to: Use chaining for short, independent steps, or for a simple success or failure check in PowerShell 7. Use an if statement when you need to test a true or false value, a number, or a string, because if tests values, while && and || test success. Do not use a long chained line for complex logic, because it is hard to read and hard to fix, and do not use && or || in Windows PowerShell 5.1, where they do not exist.

How chaining compares with the pipeline and with variables: The pipeline passes objects from one command to the next. Chaining passes nothing, it only controls order and conditions. A variable stores a value so a later command can read it, even in a chained line, because both commands share the same session. So when the second step needs data, choose the pipeline or a variable, and when the second step only needs to know whether the first step ran, or succeeded, or failed, chaining is the right tool.

Common mistakes, in plain words: The most common mistake is using && in Windows PowerShell 5.1, where it is not supported. Another is using a semicolon when the second command needs the first command output, and then wondering why the second command sees nothing. A third, and the most misleading, is using && to test a true or false answer, when && tests success, not the answer, so a False answer can still let && run. A fourth is expecting || to run when a command returns False, when || runs only on failure, not on a False answer. A fifth is writing a very long chained line that mixes order, conditions, and data, until no one can read which part depends on which.

How to spot a chaining problem: If && or || gives an error about a statement separator, check your edition, because you may be on Windows PowerShell 5.1. If the second command sees nothing, check whether you used a semicolon or a chain operator where a pipe or a variable was needed. If && runs even though the answer was False, or || does not run even though the answer was False, check whether you confused a False value with a failed command, and rewrite the test with if.

Worked examples

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

Syntax

Syntax
CommandA; CommandB

Example 1

Two independent steps, run in order. The second does not use the output of the first.

Example 1
Get-Date; Get-Service -Name Spooler

Illustrative output: Monday, 3 November 2026 10:00:00 Status Name ------ ---- Running Spooler

Example 2

PowerShell 7 only. The message prints when the Test-Connection pipeline succeeds, which normally happens when it completes without error. It does not prove the host was reachable, because && checks success, not the true or false answer. For a Reachable message based on the answer itself, use the if example below.

Example 2
Test-Connection -ComputerName localhost -Count 1 -Quiet && Write-Output "Test finished"

Illustrative output: Test finished

Example 3

This is the failure side of chaining. Get-Item tries to find a file that does not exist, and -ErrorAction SilentlyContinue hides the error message but leaves the failure flag set. The || operator then runs Write-Output, because the lookup pipeline failed. If the file existed, the lookup would succeed and || would skip the message. This operator needs PowerShell 7.

Example 3
Get-Item -Path "C:\NoSuchFolder\NoSuchFile.txt" -ErrorAction SilentlyContinue || Write-Output "Lookup failed"

Illustrative output: Lookup failed

Example 4

This is the correct way to act on a true or false answer. Test-Connection runs first, with one test to your own computer and a true or false result from -Quiet. The if statement reads that value itself, so Reachable prints only when the value is true, and Not reachable prints only when the value is false. This is different from &&, which would run on success even when the value is false.

Example 4
if (Test-Connection -ComputerName localhost -Count 1 -Quiet) { Write-Output "Reachable" } else { Write-Output "Not reachable" }

Illustrative output: Reachable

Example 5

The semicolon separates two statements in one line. The first statement stores srv1 in $server, and the second statement reads that variable, because both statements share the same session. This shows that chaining passes no pipeline data, but variables set by the first statement are still available to the second statement.

Example 5
$server = "srv1"; Write-Output "Checking $server"

Illustrative output: Checking srv1

Example 6

Three independent statements, run in order from left to right. Each output appears in the order its statement ran, and no statement receives input from the previous one. Split a line like this across several lines when it grows longer, because readability matters more than saving lines.

Example 6
Write-Output "One"; Write-Output "Two"; Write-Output "Three"

Illustrative output: One Two Three

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.

  • Using && in Windows PowerShell 5.1, where it is not supported.
  • Using a semicolon when the second command needs the first command output. Use a pipe or a variable instead.
  • Using a semicolon when the second command actually needs the output of the first, and then wondering why the second command sees nothing.
  • Using && to test a true or false answer, when && tests success, not the answer. A command that correctly returns False still counts as succeeded, so use if for true or false values.
  • Expecting || to run when a command returns False. The || operator runs only on failure, not on a False answer, so use if with an else block for False answers.
  • Writing one very long chained line for complex logic, until the order and the conditions are hard to read. Split complex steps across lines or use if statements instead.

Knowledge check

Knowledge check

What does a semicolon do between two commands?

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

Remember this and next step

Remember this: A semicolon runs commands in order, && runs the next command only on success, || runs it only on failure, success means the pipeline finished without failure as tracked by $?, not a true or false return value, use if to test a true or false value, chaining passes no data, variables still work across a chained line because they share the session, and && plus || need PowerShell 7.

Next step: Objects