Skip to content

Commit ec886a1

Browse files
Robert Holtsdwheeler
authored andcommitted
Document pipeline chain operators (&&/||) (#4856)
* Add pipeline chain doc, add to operators doc * Improve see also links * Add titles for clarity * Edit for style, etc. * Review feedback * fix typo * changed example * Add note about associativity
1 parent 7157202 commit ec886a1

File tree

2 files changed

+375
-37
lines changed

2 files changed

+375
-37
lines changed

reference/7/Microsoft.PowerShell.Core/About/about_Operators.md

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ Describes the operators that are supported by PowerShell.
1313

1414
## Long description
1515

16-
An operator is a language element that you can use in a command or
17-
expression. PowerShell supports several types of operators to help you
18-
manipulate values.
16+
An operator is a language element that you can use in a command or expression.
17+
PowerShell supports several types of operators to help you manipulate values.
1918

2019
### Arithmetic Operators
2120

2221
Use arithmetic operators (`+`, `-`, `*`, `/`, `%`) to calculate values in a
23-
command or expression. With these operators, you can add, subtract,
24-
multiply, or divide values, and calculate the remainder (modulus) of a
25-
division operation.
22+
command or expression. With these operators, you can add, subtract, multiply,
23+
or divide values, and calculate the remainder (modulus) of a division
24+
operation.
2625

2726
The addition operator concatenates elements. The multiplication operator
28-
returns the specified number of copies of each element. You can use
29-
arithmetic operators on any .NET type that implements them, such as: `Int`,
30-
`String`, `DateTime`, `Hashtable`, and Arrays.
27+
returns the specified number of copies of each element. You can use arithmetic
28+
operators on any .NET type that implements them, such as: `Int`, `String`,
29+
`DateTime`, `Hashtable`, and Arrays.
3130

3231
For more information, see [about_Arithmetic_Operators](about_Arithmetic_Operators.md).
3332

@@ -41,9 +40,9 @@ For more information, see [about_Assignment_Operators](about_Assignment_Operator
4140

4241
### Comparison Operators
4342

44-
Use comparison operators (`-eq`, `-ne`, `-gt`, `-lt`, `-le`, `-ge`) to
45-
compare values and test conditions. For example, you can compare two string
46-
values to determine whether they are equal.
43+
Use comparison operators (`-eq`, `-ne`, `-gt`, `-lt`, `-le`, `-ge`) to compare
44+
values and test conditions. For example, you can compare two string values to
45+
determine whether they are equal.
4746

4847
The comparison operators also include operators that find or replace patterns
4948
in text. The (`-match`, `-notmatch`, `-replace`) operators use regular
@@ -63,9 +62,9 @@ For more information, see [about_Comparison_Operators](about_Comparison_Operator
6362
### Logical Operators
6463

6564
Use logical operators (`-and`, `-or`, `-xor`, `-not`, `!`) to connect
66-
conditional statements into a single complex conditional. For example, you
67-
can use a logical `-and` operator to create an object filter with two
68-
different conditions.
65+
conditional statements into a single complex conditional. For example, you can
66+
use a logical `-and` operator to create an object filter with two different
67+
conditions.
6968

7069
For more information, see [about_Logical_Operators](about_logical_operators.md).
7170

@@ -81,9 +80,9 @@ For more information, see [about_Redirection](about_Redirection.md)
8180

8281
### Split and Join Operators
8382

84-
The `-split` and `-join` operators divide and combine substrings. The
85-
`-split` operator splits a string into substrings. The `-join` operator
86-
concatenates multiple strings into a single string.
83+
The `-split` and `-join` operators divide and combine substrings. The `-split`
84+
operator splits a string into substrings. The `-join` operator concatenates
85+
multiple strings into a single string.
8786

8887
For more information, see [about_Split](about_Split.md) and
8988
[about_Join](about_Join.md).
@@ -97,15 +96,15 @@ For more information, see [about_Type_Operators](about_Type_Operators.md).
9796

9897
### Unary Operators
9998

100-
Use unary operators to increment or decrement variables or object
101-
properties and to set integers to positive or negative numbers. For
102-
example, to increment the variable `$a` from `9` to `10`, you type `$a++`.
99+
Use unary operators to increment or decrement variables or object properties
100+
and to set integers to positive or negative numbers. For example, to increment
101+
the variable `$a` from `9` to `10`, you type `$a++`.
103102

104103
### Special Operators
105104

106105
Special operators have specific use-cases that do not fit into any other
107-
operator group. For example, special operators allow you to
108-
run commands, change a value's data type, or retrieve elements from an array.
106+
operator group. For example, special operators allow you to run commands,
107+
change a value's data type, or retrieve elements from an array.
109108

110109
#### Array subexpression operator `@( )`
111110

@@ -265,8 +264,8 @@ $job = Start-Job -ScriptBlock {Get-Process -Name pwsh}
265264
Receive-Job $job -Wait
266265
```
267266

268-
If you want to run multiple commands, each in their own background process
269-
but all on one line, simply place `&` between and after each of the commands.
267+
If you want to run multiple commands, each in their own background process but
268+
all on one line, simply place `&` between and after each of the commands.
270269

271270
For more information on PowerShell jobs, see [about_Jobs](about_Jobs.md).
272271

@@ -304,18 +303,18 @@ variables that the script creates are added to the current scope.
304303
> distinguish the dot from the dot (`.`) symbol that represents the current
305304
> directory.
306305
307-
In the following example, the Sample.ps1 script in the current directory is
308-
run in the current scope.
306+
In the following example, the Sample.ps1 script in the current directory is run
307+
in the current scope.
309308

310309
```powershell
311310
. .\sample.ps1
312311
```
313312

314313
#### Format operator `-f`
315314

316-
Formats strings by using the format method of string objects. Enter the
317-
format string on the left side of the operator and the objects to be
318-
formatted on the right side of the operator.
315+
Formats strings by using the format method of string objects. Enter the format
316+
string on the left side of the operator and the objects to be formatted on the
317+
right side of the operator.
319318

320319
```powershell
321320
"{0} {1,-10} {2:N}" -f 1,"hello",[math]::pi
@@ -330,10 +329,10 @@ method and [Composite Formatting](/dotnet/standard/base-types/composite-formatti
330329

331330
#### Index operator `[ ]`
332331

333-
Selects objects from indexed collections, such as arrays and hash tables.
334-
Array indexes are zero-based, so the first object is indexed as `[0]`. For
335-
arrays (only), you can also use negative indexes to get the last values.
336-
Hash tables are indexed by key value.
332+
Selects objects from indexed collections, such as arrays and hash tables. Array
333+
indexes are zero-based, so the first object is indexed as `[0]`. For arrays
334+
(only), you can also use negative indexes to get the last values. Hash tables
335+
are indexed by key value.
337336

338337
```
339338
PS> $a = 1, 2, 3
@@ -369,15 +368,35 @@ Once upon a time...
369368

370369
#### Pipeline operator `|`
371370

372-
Sends ("pipes") the output of the command that precedes it to the command
373-
that follows it. When the output includes more than one object (a
374-
"collection"), the pipeline operator sends the objects one at a time.
371+
Sends ("pipes") the output of the command that precedes it to the command that
372+
follows it. When the output includes more than one object (a "collection"), the
373+
pipeline operator sends the objects one at a time.
375374

376375
```powershell
377376
Get-Process | Get-Member
378377
Get-PSSnapin | Where-Object {$_.vendor -ne "Microsoft"}
379378
```
380379

380+
#### Pipeline chain operators `&&` and `||`
381+
382+
> [!NOTE]
383+
> This is an experimental feature. For more information see
384+
> [about_Experimental_Features](about_Experimental_Features.md).
385+
386+
Conditionally execute the right-hand side pipeline based on the success of the
387+
left-hand side pipeline.
388+
389+
```powershell
390+
# If Get-Process successfully finds a process called notepad,
391+
# Stop-Process -Name notepad is called
392+
Get-Process notepad && Stop-Process -Name notepad
393+
```
394+
395+
```powershell
396+
# If npm install fails, the node_modules directory is removed
397+
npm install || Remove-Item -Recurse ./node_modules
398+
```
399+
381400
#### Property dereferences operator `.`
382401

383402
Accesses the properties and methods of an object.
@@ -470,6 +489,8 @@ For more information, see [about_If](about_If.md).
470489

471490
[about_Type_Operators](about_Type_Operators.md)
472491

492+
[about_Pipeline_Chain_Operators](about_Pipeline_Chain_Operators.md)
493+
473494
[about_Split](about_Split.md)
474495

475496
[about_Join](about_Join.md)

0 commit comments

Comments
 (0)