How to use PowerShell Operators
By FoxLearn 8/27/2024 1:42:45 AM 108
PowerShell offers a variety of operators for performing operations on data. These operators fall into several categories, including arithmetic, comparison, logical, and more.
Assignment Operators
Operator | Example | Description |
---|---|---|
= | $x = 5 | Assigns a value to a variable. |
+= | $x += 3 | Adds the right-hand side value to the left-hand side variable and assigns the result to the variable |
-= | $x -= 3 | Subtracts the right-hand side value from the left-hand side variable and assigns the result to the variable |
*= | $x *= 3 | Multiplies the left-hand side variable by the right-hand side value and assigns the result to the variable |
/= | $x /= 3 | Divides the left-hand side variable by the right-hand side value and assigns the result to the variable |
%= | $x %= 3 | Computes the modulus (remainder) of dividing the left-hand side variable by the right-hand side value and assigns the result to the variable |
Arithmetic Operators
These operators are used for mathematical calculations
Operator | Example | Description |
---|---|---|
+ | 5 + 3 results in 8 | Addition |
- | 5 - 3 results in 2 | Subtraction |
* | 5 * 3 results in 15 | Multiplication |
/ | 5 / 2 results in 2.5 | Division |
% | 5 % 2 results in 1 | Modulus (remainder after division) |
++ | $x++ increments $x by 1 | Increment |
-- | $x-- decrements $x by 1 | Decrement |
Comparison Operators
These operators compare values and return a Boolean result
Operator | Example | Description |
---|---|---|
-eq | 5 -eq 5 returns True | Equal |
-ne | 5 -ne 3 returns True | Not equal |
-gt | 5 -gt 3 returns True | Greater than |
-lt | 5 -lt 3 returns False | Less than |
-ge | 5 -ge 5 returns True | Greater than or equal to |
-le | 5 -le 6 returns True | Less than or equal to |
-like | "hello" -like "h*" returns True | Matches a pattern |
-notlike | "hello" -notlike "h*" returns False | Does not match a pattern |
-match | "hello" -match "ell" returns True | Matches a regular expression |
-notmatch | "hello" -notmatch "abc" returns True | Does not match a regular expression |
Logical Operators
These operators are used to perform logical operations
Operator | Example | Description |
---|---|---|
-and | ($true -and $false) returns False | Logical AND |
-or | ($true -or $false) returns True | Logical OR |
-xor | ($true -xor $false) returns True | Logical XOR |
-not | -not $true returns False | Logical NOT |
! | !$true returns False | Negation |
Array Operators
These operators are used with arrays
Operator | Example | Description |
---|---|---|
+ | $a + $b concatenates arrays $a and $b | Array concatenation |
- | $a - $b returns elements in $a but not in $b | Array subtraction |
Type Operators
These operators are used for type checking and conversion
Operator | Example | Description |
---|---|---|
-is | $var -is [int] checks if $var is an integer | Checks if a variable is of a specified type. |
-as | $var -as [int] attempts to cast $var to an integer | Tries to convert a variable to a specified type. |
Redirection Operators
These operators are used for redirecting output
Operator | Example | Description |
---|---|---|
> | Get-Process > processes.txt | Redirect output to a file |
>> | Get-Process >> processes.txt | Append output to a file |
2> | Get-Process 2> errors.txt | Redirect error output to a file |
2>> | Get-Process 2>> errors.txt | Append error output to a file |
- How to display GUI Message Boxes in PowerShell
- How to sign a powershell script
- How to run powershell script using task scheduler
- How to run powershell script from cmd
- How to run powershell commands in C#
- How to execute PowerShell script in C#
- How to delete a Windows service in PowerShell
- How to download file from url in PowerShell
Categories
Popular Posts
Monster Admin Template
11/14/2024
Material Admin Dashboard Template
11/15/2024
Responsive Admin Dashboard Template
11/11/2024
Freedash bootstrap lite
11/13/2024