You are currently viewing Operators In PowerShell

Operators In PowerShell

Operators In PowerShell

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed variables in PowerShell.

https://cloudaffaire.com/variables-in-powershell/

In this blog post, we will discuss operators in PowerShell. Like any other programming language, PowerShell also supports operator. Operators are constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. Common simple examples include arithmetic (addition with +), comparison (equality with -eq), and logical operations (such as -and) etc. The operator tells the interpreter/compiler to perform some mathematical or logical construct on the operands.

Operators In PowerShell:

Arithmetic Operators:

Arithmetic operators are used to perform arithmetic operations like addition, subtraction, multiplication, division etc. Some of the arithmetic operators can also be used on string and other data types apart from regular numbers. For example, 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.

Operator Description
+ Adds integers and concatenates strings, arrays, and hash tables.
Subtracts one value from another and negative number assignment
* Multiply numbers and copy strings and arrays the specified number of times.
/ Divides two values.
% returns the remainder of a division operation.
-band Bitwise AND
-bnot Bitwise NOT
-bor Bitwise OR
-bxor Bitwise XOR

 

Assignment Operators:

Assignment operators can be used to assign one or more values to a variable. The existing value of a variable can also be changed using assignment operators. Below is the list of assignment operators available in PowerShell.

Operator Description
= Sets the value of a variable to the specified value.
+= Increases the value of a variable by the specified value, or appends the specified value to the existing value.
-= Decreases the value of a variable by the specified value.
*= Multiplies the value of a variable by the specified value, or appends the specified value to the existing value.
/= Divides the value of a variable by the specified value.
%= Divides the value of a variable by the specified value and then assigns the remainder (modulus) to the variable.
++ Increases the value of a variable, assignable property, or array element by 1.
Decreases the value of a variable, assignable property, or array element by 1.

 

Comparison Operators:

Comparison operators can be used to compare the values of two or more operands. Below is the list of all comparison operators available in PowerShell.

Type Operators Description
Equality -eq equals
-ne not equals
-gt greater than
-ge greater than or equal
-lt less than
-le less than or equal
Matching -like Returns true when string matches wildcard pattern
-notlike Returns true when string does not match wildcard pattern
-match Returns true when string matches regex pattern, $matches contains matching strings
-notmatch Returns true when string does not match regex pattern; $matches contains matching string
Containment -contains Returns true when reference value contained in a collection
-notcontains Returns true when reference value not contained in a collection
-in Returns true when test value contained in a collection
-notin Returns true when test value not contained in a collection
Replacement -replace Replaces a string pattern
Type -is Returns true if both objects are the same type
-isnot Returns true if the objects are not the same type

 

Logical Operators:

Logical operators are used to connect conditional statements into a single complex conditional. Below is the list of logical operators available in PowerShell.

Operator Description
-and Logical AND. TRUE when both statements are TRUE.
-or Logical OR. TRUE when either statement is TRUE.
-xor Logical EXCLUSIVE OR. TRUE when only one statement is TRUE
-not Logical not. Negates the statement that follows
! Same as -not

 

Redirection Operators:

By default, PowerShell sends its command output to the PowerShell console. However, you can direct the output to a text file, and you can redirect error output to the regular output stream. The redirection operators enable you to send streams of data to a file or the Success output stream. The PowerShell redirection operators use the following numbers to represent the available output streams –

Stream (n) Description
1 Success Stream
2 Error Stream
3 Warning Stream
4 Verbose Stream
5 Debug Stream
6 Information Stream
* All Streams

 

Below is the list of available redirection operators in PowerShell

Operator Description
> Send specified stream to a file.
>> Append specified stream to a file.
n>&1 Redirects the specified stream (n) to the Success stream (1)

 

Other Special Operators:

PowerShell also provides some other special operators which 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. Below is the list of some of the special operators available in PowerShell –

Type Description Syntax
Split Operator Splits one or more strings into substrings. -Split <String>
-Split (<String[]>)
<String> -Split <Delimiter>[,<Max-substrings>[,”<Options>”]]
<String> -Split {<ScriptBlock>} [,<Max-substrings>]
Join Operator Concatenates a set of strings into a single string -Join <String[]>
<String[]> -Join <Delimiter>
Type Operator Returns TRUE when the input is an instance of the specified .NET type. -is
Returns TRUE when the input is not an instance of the specified .NET type. -isNot
Converts the input to the specified .NET type. -as
Grouping Operator Groups multiple expression or let output from a command participate in an expression (..)
Subexpression Operator Returns the result of one or more statements. For a single result, returns a scalar. For multiple results, returns an array. $(..)
Array Subexpression Operator Returns the result of one or more statements as an array. @(..)
Call Operator also known as the “invocation operator”, lets you run commands that are stored in variables and represented by strings or script blocks. &
Cast Operator Converts or limits objects to the specified type. [<type>]
Comma Operator As a binary operator, the comma creates an array. As a unary operator, the comma creates an array with one member ,
Dot Sourcing Operator Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope. .
Index Operator Selects objects from indexed collections, such as arrays and hash tables. [..]
Pipeline Operator Sends (“pipes”) the output of the command that precedes it to the command that follows it. |
Range Operator Represents the sequential integers in an integer array, given an upper, and lower boundary. <upper_boundary>..<lower_boundary>
Member Access Operator Accesses the properties and methods of an object. .
Static Member Operator Calls the static properties and methods of a .NET Framework class. ::

 

Hope you have enjoyed this article. In the next blog post, we will discuss Conditions in PowerShell.

To get more details on PowerShell, kindly follow below official documentation

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about?view=powershell-5.1

 

Leave a Reply