Skip to content

Commit 9e43a44

Browse files
author
Sean Wheeler
committed
Edit for style, etc.
1 parent e0e8d99 commit 9e43a44

File tree

2 files changed

+93
-88
lines changed

2 files changed

+93
-88
lines changed

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

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ Describes the operators that are supported by PowerShell.
1212

1313
## Long description
1414

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

1918
### Arithmetic Operators
2019

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

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

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

@@ -40,9 +39,9 @@ For more information, see [about_Assignment_Operators](about_Assignment_Operator
4039

4140
### Comparison Operators
4241

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

4746
The comparison operators also include operators that find or replace patterns
4847
in text. The (`-match`, `-notmatch`, `-replace`) operators use regular
@@ -51,17 +50,17 @@ expressions, and (`-like`, `-notlike`) use wildcards `*`.
5150
Containment comparison operators determine whether a test value appears in a
5251
reference set (`-in`, `-notin`, `-contains`, `-notcontains`).
5352

54-
Bitwise comparison operators (`-bAND`, `-bOR`, `-bXOR`, `-bNOT`) manipulate
55-
the bit patterns in values.
53+
Bitwise comparison operators (`-bAND`, `-bOR`, `-bXOR`, `-bNOT`) manipulate the
54+
bit patterns in values.
5655

5756
For more information, see [about_Comparison_Operators](about_Comparison_Operators.md).
5857

5958
### Logical Operators
6059

6160
Use logical operators (`-and`, `-or`, `-xor`, `-not`, `!`) to connect
62-
conditional statements into a single complex conditional. For example, you
63-
can use a logical `-and` operator to create an object filter with two
64-
different conditions.
61+
conditional statements into a single complex conditional. For example, you can
62+
use a logical `-and` operator to create an object filter with two different
63+
conditions.
6564

6665
For more information, see [about_Logical_Operators](about_logical_operators.md).
6766

@@ -77,9 +76,9 @@ For more information, see [about_Redirection](about_Redirection.md)
7776

7877
### Split and Join Operators
7978

80-
The `-split` and `-join` operators divide and combine substrings. The
81-
`-split` operator splits a string into substrings. The `-join` operator
82-
concatenates multiple strings into a single string.
79+
The `-split` and `-join` operators divide and combine substrings. The `-split`
80+
operator splits a string into substrings. The `-join` operator concatenates
81+
multiple strings into a single string.
8382

8483
For more information, see [about_Split](about_Split.md) and
8584
[about_Join](about_Join.md).
@@ -93,15 +92,15 @@ For more information, see [about_Type_Operators](about_Type_Operators.md).
9392

9493
### Unary Operators
9594

96-
Use unary operators to increment or decrement variables or object
97-
properties and to set integers to positive or negative numbers. For
98-
example, to increment the variable `$a` from `9` to `10`, you type `$a++`.
95+
Use unary operators to increment or decrement variables or object properties
96+
and to set integers to positive or negative numbers. For example, to increment
97+
the variable `$a` from `9` to `10`, you type `$a++`.
9998

10099
### Special Operators
101100

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

106105
#### Array subexpression operator `@( )`
107106

@@ -261,8 +260,8 @@ $job = Start-Job -ScriptBlock {Get-Process -Name pwsh}
261260
Receive-Job $job -Wait
262261
```
263262

264-
If you want to run multiple commands, each in their own background process
265-
but all on one line, simply place `&` between and after each of the commands.
263+
If you want to run multiple commands, each in their own background process but
264+
all on one line, simply place `&` between and after each of the commands.
266265

267266
For more information on PowerShell jobs, see [about_Jobs](about_Jobs.md).
268267

@@ -300,18 +299,18 @@ variables that the script creates are added to the current scope.
300299
> distinguish the dot from the dot (`.`) symbol that represents the current
301300
> directory.
302301
303-
In the following example, the Sample.ps1 script in the current directory is
304-
run in the current scope.
302+
In the following example, the Sample.ps1 script in the current directory is run
303+
in the current scope.
305304

306305
```powershell
307306
. .\sample.ps1
308307
```
309308

310309
#### Format operator `-f`
311310

312-
Formats strings by using the format method of string objects. Enter the
313-
format string on the left side of the operator and the objects to be
314-
formatted on the right side of the operator.
311+
Formats strings by using the format method of string objects. Enter the format
312+
string on the left side of the operator and the objects to be formatted on the
313+
right side of the operator.
315314

316315
```powershell
317316
"{0} {1,-10} {2:N}" -f 1,"hello",[math]::pi
@@ -326,10 +325,10 @@ method and [Composite Formatting](/dotnet/standard/base-types/composite-formatti
326325

327326
#### Index operator `[ ]`
328327

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

334333
```
335334
PS> $a = 1, 2, 3
@@ -365,9 +364,9 @@ Once upon a time...
365364

366365
#### Pipeline operator `|`
367366

368-
Sends ("pipes") the output of the command that precedes it to the command
369-
that follows it. When the output includes more than one object (a
370-
"collection"), the pipeline operator sends the objects one at a time.
367+
Sends ("pipes") the output of the command that precedes it to the command that
368+
follows it. When the output includes more than one object (a "collection"), the
369+
pipeline operator sends the objects one at a time.
371370

372371
```powershell
373372
Get-Process | Get-Member
@@ -376,8 +375,12 @@ Get-PSSnapin | Where-Object {$_.vendor -ne "Microsoft"}
376375

377376
#### Pipeline chain operators `&&` and `||`
378377

379-
Conditionally execute the right-hand side pipeline
380-
based on the success of the left-hand side pipeline.
378+
> [!NOTE]
379+
> This is an experimental feature. For more information see
380+
> [about_Experimental_Features](about_Experimental_Features.md).
381+
382+
Conditionally execute the right-hand side pipeline based on the success of the
383+
left-hand side pipeline.
381384

382385
```powershell
383386
# If Get-Process successfully finds a process called notepad,

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

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,32 @@ Describes chaining pipelines with the `&&` and `||` operators in PowerShell.
1414

1515
## Long description
1616

17-
From PowerShell 7, PowerShell implements the `&&` and `||` operators
18-
to conditionally chain pipelines.
19-
These operators are known in PowerShell as *pipeline chain operators*,
20-
and are similar to [AND-OR lists](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_03)
21-
in POSIX shells like bash, zsh and sh,
22-
as well as [conditional processing symbols](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490954(v=technet.10)#using-multiple-commands-and-conditional-processing-symbols)
23-
in Windows' command shell (cmd.exe).
24-
25-
`&&` and `||` allow conditional execution of PowerShell commands and pipelines
26-
based on the success of the left-hand pipeline.
27-
In particular, they allow conditional execution of native executables
28-
based on execution success:
17+
Beginning in PowerShell 7, PowerShell implements the `&&` and `||` operators to
18+
conditionally chain pipelines. These operators are known in PowerShell as
19+
*pipeline chain operators*, and are similar to
20+
[AND-OR lists](https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_03)
21+
in POSIX shells like bash, zsh and sh, as well as
22+
[conditional processing symbols](/previous-versions/windows/it-pro/windows-xp/bb490954(v=technet.10)#using-multiple-commands-and-conditional-processing-symbols)
23+
in the Windows Command Shell (cmd.exe).
24+
25+
The `&&` operator executes the right-hand pipeline, if the left-hand pipeline
26+
succeeded. Conversely, the `||` operator executes the right-hand pipeline if
27+
the left-hand pipeline failed.
28+
29+
This can be use for conditional execution of software deployments. For example:
2930

3031
```powershell
3132
npm run build && npm run deploy
3233
```
3334

34-
The `&&` operator will execute the right-hand pipeline,
35-
if the left-hand pipeline succeeded,
36-
whereas the `||` operator will execute the right-hand pipeline
37-
if the left-hand pipeline failed.
35+
In this example the `npm run deploy` command only runs if the build command
36+
succeeds.
37+
38+
> [!NOTE]
39+
> This is an experimental feature. For more information see
40+
> [about_Experimental_Features](about_Experimental_Features.md).
41+
42+
### Examples
3843

3944
#### Two successful commands
4045

@@ -59,7 +64,7 @@ Write-Error 'Bad' && Write-Output 'Second' : Bad
5964
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
6065
```
6166

62-
#### First command succeeds, so `||` means second command not executed
67+
#### First command succeeds, so the second command is not executed
6368

6469
```powershell
6570
Write-Output 'First' || Write-Output 'Second'
@@ -69,7 +74,7 @@ Write-Output 'First' || Write-Output 'Second'
6974
First
7075
```
7176

72-
#### First command fails, so `||` means second command is executed
77+
#### First command fails, so the second command is executed
7378

7479
```powershell
7580
Write-Error 'Bad' || Write-Output 'Second'
@@ -83,8 +88,8 @@ Write-Error 'Bad' && Write-Output 'Second' : Bad
8388
Second
8489
```
8590

86-
Pipeline success is defined by the value of the `$?` variable,
87-
which PowerShell automatically sets after executing a pipeline based on its execution status.
91+
Pipeline success is defined by the value of the `$?` variable, which PowerShell
92+
automatically sets after executing a pipeline based on its execution status.
8893
This means that pipeline chain operators have the following equivalence:
8994

9095
```powershell
@@ -111,8 +116,8 @@ Test-Command '1'; if (-not $?) { Test-Command '2' }
111116

112117
### Assignment from pipeline chains
113118

114-
Assigning a variable from a pipeline chain takes the concatenation
115-
of all the pipelines in the chain:
119+
Assigning a variable from a pipeline chain takes the concatenation of all the
120+
pipelines in the chain:
116121

117122
```powershell
118123
$result = Write-Output '1' && Write-Output '2'
@@ -124,8 +129,8 @@ $result
124129
2
125130
```
126131

127-
If a script-terminating error is thrown during assignment from a pipeline chain,
128-
the assignment does not succeed:
132+
If a script-terminating error is thrown during assignment from a pipeline
133+
chain, the assignment does not succeed:
129134

130135
```powershell
131136
try
@@ -146,24 +151,24 @@ Result:
146151

147152
### Operator syntax and precedence
148153

149-
Unlike other operators, `&&` and `||` operate on pipelines,
150-
rather than on expressions (like, for example, `+` or `-and`).
154+
Unlike other operators, `&&` and `||` operate on pipelines, rather than on
155+
expressions like `+` or `-and`, for example.
151156

152157
`&&` and `||` have a lower precedence than piping (`|`) or redirection (`>`),
153-
but a higher precdence than job operators (`&`), assignment (`=`) or semicolons (`;`).
154-
This means that pipelines within a pipeline chain can be individually redirected,
155-
and that entire pipeline chains can be backgrounded, assigned from
156-
or separated as statements.
158+
but a higher precedence than job operators (`&`), assignment (`=`) or
159+
semicolons (`;`). This means that pipelines within a pipeline chain can be
160+
individually redirected, and that entire pipeline chains can be backgrounded,
161+
assigned to variables, or separated as statements.
157162

158-
To use lower precedence syntax within a pipeline chain,
159-
consider the use of parentheses `(...)` or a subexpression `$(...)`.
163+
To use lower precedence syntax within a pipeline chain, consider the use of
164+
parentheses `(...)` or a subexpression `$(...)`.
160165

161166
### Error interaction
162167

163-
Pipeline chain operators, particularly the `||` operator,
164-
do not absorb errors.
165-
If a statement in a pipeline chain throws a script-terminating error,
166-
that will abort the pipeline chain:
168+
Pipeline chain operators do not absorb errors. When a statement in a pipeline
169+
chain throws a script-terminating error, the pipeline chain is terminated.
170+
171+
For example:
167172

168173
```powershell
169174
$(throw 'Bad') || Write-Output '2'
@@ -178,7 +183,7 @@ At line:1 char:3
178183
+ FullyQualifiedErrorId : Bad
179184
```
180185

181-
If the error is caught, the pipeline chain will still be cut short:
186+
Even when the error is caught, the pipeline chain is still terminated:
182187

183188
```powershell
184189
try
@@ -197,9 +202,8 @@ Caught: Bad
197202
Done
198203
```
199204

200-
If an error is non-terminating,
201-
or only terminates a pipeline,
202-
the pipeline chain will continue, respecting on `$?`:
205+
If an error is non-terminating, or only terminates a pipeline, the pipeline
206+
chain continues, respecting the value of `$?`:
203207

204208
```powershell
205209
function Test-NonTerminatingError
@@ -232,10 +236,9 @@ Second
232236

233237
### Chaining pipelines rather than commands
234238

235-
Pipeline chain operators, by their name, can be used to chain pipelines,
236-
rather than just commands.
237-
This matches the behaviour of other shells,
238-
but can make "success" harder to reason about:
239+
Pipeline chain operators, by their name, can be used to chain pipelines, rather
240+
than just commands. This matches the behavior of other shells, but can make
241+
*success* harder to determine:
239242

240243
```powershell
241244
function Test-NotTwo
@@ -278,9 +281,8 @@ At line:1 char:9
278281
3
279282
```
280283

281-
Note that `Write-Output 'All done!'` is not executed,
282-
since `Test-NotTwo` is deemed to have failed
283-
after throwing the non-terminating error.
284+
Note that `Write-Output 'All done!'` is not executed, since `Test-NotTwo` is
285+
deemed to have failed after throwing the non-terminating error.
284286

285287
## See also
286288

0 commit comments

Comments
 (0)