PowerShell Operators

Operators are symbols or keywords that you use in PowerShell commands to perform operations. These operations can include comparisons, arithmetic calculations, logical evaluations, and more. Understanding PowerShell operators is crucial for writing efficient and powerful scripts.

Arithmetic Operators

These operators are used for mathematical calculations.

Addition

+

Adds two numbers or concatenates strings.

Subtraction

-

Subtracts the right operand from the left operand.

Multiplication

*

Multiplies two numbers or repeats a string.

Division

/

Divides the left operand by the right operand.

Modulus

%

Returns the remainder of a division.

Comparison Operators

These operators compare values. They return a Boolean value ($true or $false).

Equal To

-eq

Checks if two values are equal.

Not Equal To

-ne

Checks if two values are not equal.

Greater Than

-gt

Checks if the left value is greater than the right value.

Greater Than or Equal To

-ge

Checks if the left value is greater than or equal to the right value.

Less Than

-lt

Checks if the left value is less than the right value.

Less Than or Equal To

-le

Checks if the left value is less than or equal to the right value.

Like

-like

Checks if a string matches a wildcard pattern.

Not Like

-notlike

Checks if a string does not match a wildcard pattern.

Match

-match

Checks if a string matches a regular expression.

Not Match

-notmatch

Checks if a string does not match a regular expression.

Logical Operators

These operators combine Boolean expressions.

And

-and

Returns true if both conditions are true.

Or

-or

Returns true if either condition is true.

Not (Negation)

-not

Returns true if the condition is false.

Xor (Exclusive Or)

-xor

Returns true if exactly one of the conditions is true.

Assignment Operators

These operators assign values to variables.

Assign

=

Assigns a value to a variable.

Add and Assign

+=

Adds the right operand to the left operand and assigns the result to the left operand.

Subtract and Assign

-=

Subtracts the right operand from the left operand and assigns the result to the left operand.

Multiply and Assign

*=

Multiplies the left operand by the right operand and assigns the result to the left operand.

Divide and Assign

/=

Divides the left operand by the right operand and assigns the result to the left operand.

Type Operators

These operators test or cast values to specific types.

Is

-is

Checks if an object is of a specified .NET type.

Is NOT

-isnot

Checks if an object is NOT of a specified .NET type.

As

-as

Casts an object to a specified .NET type, returning null if the cast fails.

Redirection Operators

These operators redirect command output.

Output to File

>

Redirects output to a file (overwrites).

Append Output to File

>>

Appends output to a file.

Error Output to File

2>

Redirects error output to a file.

Miscellaneous Operators

A variety of other useful operators.

Member Access

.

Accesses properties and methods of an object.

Index Access

[]

Accesses elements of an array or collection by index.

Pipeline

|

Sends the output of one command as input to another command.

Range

..

Creates a sequence of numbers.

Format (String)

-f

Formats strings using .NET string formatting.

Tip: For complex comparisons involving wildcards or regular expressions, PowerShell's comparison operators are case-insensitive by default. Use the uppercase versions (e.g., -Like, -Match) for case-sensitive operations.

Mastering these operators will significantly enhance your ability to automate tasks and manage systems using PowerShell.

Example Usage

# Check if a number is even
$number = 10
if ($number % 2 -eq 0) {
    Write-Host "$number is even."
}

# String comparison
$name = "PowerShell"
if ($name -like "Power*") {
    Write-Host "The name starts with 'Power'."
}

# Using the pipeline
Get-Process | Where-Object {$_.CPU -gt 100} | Select-Object Name, CPU