@@ -13,21 +13,20 @@ Describes the operators that are supported by PowerShell.
13
13
14
14
## Long description
15
15
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.
19
18
20
19
### Arithmetic Operators
21
20
22
21
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.
26
25
27
26
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.
31
30
32
31
For more information, see [ about_Arithmetic_Operators] ( about_Arithmetic_Operators.md ) .
33
32
@@ -41,9 +40,9 @@ For more information, see [about_Assignment_Operators](about_Assignment_Operator
41
40
42
41
### Comparison Operators
43
42
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.
47
46
48
47
The comparison operators also include operators that find or replace patterns
49
48
in text. The (` -match ` , ` -notmatch ` , ` -replace ` ) operators use regular
@@ -63,9 +62,9 @@ For more information, see [about_Comparison_Operators](about_Comparison_Operator
63
62
### Logical Operators
64
63
65
64
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.
69
68
70
69
For more information, see [ about_Logical_Operators] ( about_logical_operators.md ) .
71
70
@@ -81,9 +80,9 @@ For more information, see [about_Redirection](about_Redirection.md)
81
80
82
81
### Split and Join Operators
83
82
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.
87
86
88
87
For more information, see [ about_Split] ( about_Split.md ) and
89
88
[ about_Join] ( about_Join.md ) .
@@ -97,15 +96,15 @@ For more information, see [about_Type_Operators](about_Type_Operators.md).
97
96
98
97
### Unary Operators
99
98
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++ ` .
103
102
104
103
### Special Operators
105
104
106
105
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.
109
108
110
109
#### Array subexpression operator ` @( ) `
111
110
@@ -265,8 +264,8 @@ $job = Start-Job -ScriptBlock {Get-Process -Name pwsh}
265
264
Receive-Job $job -Wait
266
265
```
267
266
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.
270
269
271
270
For more information on PowerShell jobs, see [ about_Jobs] ( about_Jobs.md ) .
272
271
@@ -304,18 +303,18 @@ variables that the script creates are added to the current scope.
304
303
> distinguish the dot from the dot (` . ` ) symbol that represents the current
305
304
> directory.
306
305
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.
309
308
310
309
``` powershell
311
310
. .\sample.ps1
312
311
```
313
312
314
313
#### Format operator ` -f `
315
314
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.
319
318
320
319
``` powershell
321
320
"{0} {1,-10} {2:N}" -f 1,"hello",[math]::pi
@@ -330,10 +329,10 @@ method and [Composite Formatting](/dotnet/standard/base-types/composite-formatti
330
329
331
330
#### Index operator ` [ ] `
332
331
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.
337
336
338
337
```
339
338
PS> $a = 1, 2, 3
@@ -369,15 +368,35 @@ Once upon a time...
369
368
370
369
#### Pipeline operator ` | `
371
370
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.
375
374
376
375
``` powershell
377
376
Get-Process | Get-Member
378
377
Get-PSSnapin | Where-Object {$_.vendor -ne "Microsoft"}
379
378
```
380
379
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
+
381
400
#### Property dereferences operator ` . `
382
401
383
402
Accesses the properties and methods of an object.
@@ -470,6 +489,8 @@ For more information, see [about_If](about_If.md).
470
489
471
490
[ about_Type_Operators] ( about_Type_Operators.md )
472
491
492
+ [ about_Pipeline_Chain_Operators] ( about_Pipeline_Chain_Operators.md )
493
+
473
494
[ about_Split] ( about_Split.md )
474
495
475
496
[ about_Join] ( about_Join.md )
0 commit comments