Skip to content

Document pipeline chain operators (&&/||) #4856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 60 additions & 39 deletions reference/7/Microsoft.PowerShell.Core/About/about_Operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ Describes the operators that are supported by PowerShell.

## Long description

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

### Arithmetic Operators

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

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

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

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

### Comparison Operators

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

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

Bitwise comparison operators (`-bAND`, `-bOR`, `-bXOR`, `-bNOT`) manipulate
the bit patterns in values.
Bitwise comparison operators (`-bAND`, `-bOR`, `-bXOR`, `-bNOT`) manipulate the
bit patterns in values.

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

### Logical Operators

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

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

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

### Split and Join Operators

The `-split` and `-join` operators divide and combine substrings. The
`-split` operator splits a string into substrings. The `-join` operator
concatenates multiple strings into a single string.
The `-split` and `-join` operators divide and combine substrings. The `-split`
operator splits a string into substrings. The `-join` operator concatenates
multiple strings into a single string.

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

### Unary Operators

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

### Special Operators

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

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

Expand Down Expand Up @@ -261,8 +260,8 @@ $job = Start-Job -ScriptBlock {Get-Process -Name pwsh}
Receive-Job $job -Wait
```

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

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

Expand Down Expand Up @@ -300,18 +299,18 @@ variables that the script creates are added to the current scope.
> distinguish the dot from the dot (`.`) symbol that represents the current
> directory.

In the following example, the Sample.ps1 script in the current directory is
run in the current scope.
In the following example, the Sample.ps1 script in the current directory is run
in the current scope.

```powershell
. .\sample.ps1
```

#### Format operator `-f`

Formats strings by using the format method of string objects. Enter the
format string on the left side of the operator and the objects to be
formatted on the right side of the operator.
Formats strings by using the format method of string objects. Enter the format
string on the left side of the operator and the objects to be formatted on the
right side of the operator.

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

#### Index operator `[ ]`

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

```
PS> $a = 1, 2, 3
Expand Down Expand Up @@ -365,15 +364,35 @@ Once upon a time...

#### Pipeline operator `|`

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

```powershell
Get-Process | Get-Member
Get-PSSnapin | Where-Object {$_.vendor -ne "Microsoft"}
```

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

> [!NOTE]
> This is an experimental feature. For more information see
> [about_Experimental_Features](about_Experimental_Features.md).

Conditionally execute the right-hand side pipeline based on the success of the
left-hand side pipeline.

```powershell
# If Get-Process successfully finds a process called notepad,
# Stop-Process -Name notepad is called
Get-Process notepad && Stop-Process -Name notepad
```

```powershell
# If npm install fails, the node_modules directory is removed
npm install || Remove-Item -Recurse ./node_modules
```

#### Property dereferences operator `.`

Accesses the properties and methods of an object.
Expand Down Expand Up @@ -466,6 +485,8 @@ For more information, see [about_If](about_If.md).

[about_Type_Operators](about_Type_Operators.md)

[about_Pipeline_Chain_Operators](about_Pipeline_Chain_Operators.md)

[about_Split](about_Split.md)

[about_Join](about_Join.md)
Expand Down
Loading