EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Operators
Secondary Sidebar
PowerShell Tutorial
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

PowerShell Operators

By Priya PedamkarPriya Pedamkar

PowerShell Operators

What are PowerShell Operators?

We mostly use PowerShell Operators to execute commands. If you want to do something you will search for that command on the web and copy the command and paste it in either command prompt or PowerShell. But truly PowerShell is an object-oriented automation tool rather than a DOS Command-line interface.

PowerShell Operators

In this section, I will explain the commonly used operators supported by PowerShell. But It has a lot of operators. that I listed below,

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  1. Arithmetic Operators (+, -, *, /, %)
  2. Assignment Operators (=, +=, -=, *=, /=, %=)
  3. Comparison Operators
    • Equality Operators (-eq, -ne, -gt, -lt, -le, -ge)
    • Matching Operators(-match, -notmatch, -replace)
    • Containment comparison Operators (-in, -notin, -contains, -notcontains)
  4. Logical Operators (-and, -or, -xor, -not, !)
  5. Redirection Operators (>, >>, and >&1)
  6. Split and Join Operators (-split, -join)
  7. Type Operators (-is, -isnot, -as)
  8. Unary Operators (++, –)
  9. Special Operators

Different Types of PowerShell Operators

PowerShell Operators are easy to use. Here we will discuss how to use PowerShell Operators with the help of examples.

1. Arithmetic Operators

Arithmetic operators calculate numeric values. You can do addition, subtraction, multiplication, division, remainder operation. In addition + and * operates on string, arrays and hash tables.

Operator Description Example
+ Adds numeric values 6 + 2
concatenates a string, arrays, and hash tables “edu” + “CBA”
– Subtracts numeric values 8 – 9
Makes a number negative -98
* Multiple numeric values 6 * 2
copy string, arrays to the specified number of times “!” * 3
/ Divides numeric values 9 / 7
% Gives remainder after division 9 % 3

2. Assignment Operators

PowerShell Assignment operators assign, change or append values to variables.

Operator Description Example
= Assign value to variable $a = 3
+= Adds and assign value to the variable $a += 4
Concatenates string at the end $b = “Hello, “
$b += “World”
Adds number to array $a = 1,2,3
$a += 2
-= Subtracts and assign value to the variable $a -= 9
*= Multiplies and assign value to the variable $a *= 2
Appends string with a specified number of times $e = “String “
$e * 5
/= Divides and assign a value to the variable $a /= 7
%= Divides and assign a remainder value to the variable $a %= 3

3. Comparison operators

  • Equality Operators

Check for equality of values. This includes numeric, strings, array. It will return True or False are a result.

Operator Description Example
-eq Check for equal value 1 -eq 1
Check for equal arrays 1,2,3 -eq 2
Check for equal strings “Hello” -eq “World”
-ne Check for non-equal value 1 -ne 2
Check for non-equal arrays 1,2,3 -ne 2
Check for non-equal strings “Hello” -ne “World”
-gt Check for greater value 8 -gt 6
Check all greater values in array and prints one by one 7, 8, 9 -gt 8
-ge Check for greater or equal value 8 -ge 8
Check all greater values or equal values in array and prints one by one 7, 8, 9 -ge 8
-lt Check for lesser value 8 -lt 6
Check all lesser values in array and prints one by one 7, 8, 9 -lt 8
-le Check for lesser or equal value 6 -le 8
Check all lesser values or equal values in array and prints one by one 7, 8, 9 -le 8
  • Matching Operators

These PowerShell operators are capable of finding elements with specific patterns using wild card expressions.

Operator Description Example
-match Matches a string with a specified regular expression “Sunday”, “Monday”, “Tuesday” -match “sun”
-notmatch Does not match a string with a specified regular expression “Sunday”, “Monday”, “Tuesday” -notmatch “sun”
-replace Check for the given string and replace with specified string “book” -replace “B”, “C”
  • Containment Comparison Operators

These PowerShell Operators are used to checks for the existence of a specified element or array in an array.

Operator Description Example
-contains Checks for the existence of a specified element in an array “red”, “yellow” -contains “red”
-notcontains Checks for the non-existence of specified element in an array “red”, “yellow” -notcontains “green”
-in Checks for the existence of a specified element in an array “red” -in “red”, “yellow”
-notin Checks for the non-existence of specified element in an array “green” -notin “red”, “yellow”

Note: both contain and in do the same operation, the operand order differs, in “contains” we take right-hand value to check against left-hand value. But in “in” we take left-hand value to check against right-hand value.

4. Logical Operators

It also allows us to use logical operations like AND, OR, NOT, XOR.

Operator Description Example
-and Truth with both statements is TRUE. 1 -and 1
-or Truth with any one of the statements is TRUE. 1 -or 0
-xor Truth when only of the statement is TRUE. 1 -xor 0
-not Negates the statement. -not 1
! Negates the statement !0

5. Redirection Operator

These are used to redirect the output of one command as the input to another command.

Operator Description Example
> Send all success stream data to output .\script.ps1 > script.log
>> Appends all success stream data to output .\script.ps1 >> script.log
n>&1 Redirects a specified stream (n) to output .\script.ps1 3>&1 script.log

Note:

  • 3>&1 – is for warning redirection
  • 2>&1 – is for error redirection

6. Split and Join Operator

This PowerShell operator used to redirect the output of one command as the input to another command.’

Operator Description Example
-split Splits a string into to substring based on a delimiter -split “one two three four”
Splits string with a specified delimiter “Lastname:FirstName:Address” -split “:”
-join Joins given strings to a single string -join “a”, “b”, “c”

7. Type Operators

Type operators tell whether the given object is an instance of a specified .NET type.

Operator Description Example
-is Compares instance of the specified .NET type and return True if equal. 32 -is “int”
-isNot Compares the instance of specified .NET and return False if not equal. 32 -isNot “int”
-as Converts the given value to the specified type “12/31/07” -as [DateTime]

8. Unary Operators

These are the quick operators for increment and decrement in PowerShell. Mostly used on iteration.

Operator Description Example
++ Increments value by 1and assign back $a = 9

$a++

— Decrement value by 1 and assign back $a = 9

$a–

9. Special Operators

PowerShell Special operators have specific use cases that do not fit in any other group.

Operator Description Example
@( ) Display the result of one or more statements as a list. @(Get-WmiObject win32_logicalDisk)
& Runs a command, script or block. $c = “get-executionpolicy”

& $c

[ ] Converts object to specified type [int64]$a = 34
, Comma creates an array. $myArray = 1,2,3
-f Format operator, formats string by format methods “{0} {1,-10} {2:N}” -f 1,”hello”,[math]::pi
.. Generates a range ‘a’..’f’

Recommended Articles

This has been a guide to PowerShell Operators. Here we have discussed the basic concept along with various types of PowerShell Operators in detail. You can also go through our other suggested articles to learn more –

  1. Uses Of Powershell
  2. Windows Operators
  3. C Operators
  4. MySQL Operators
Popular Course in this category
All in One Data Science Bundle (360+ Courses, 50+ projects)
  360+ Online Courses |  1500+ Hours |  Verifiable Certificates |  Lifetime Access
4.7
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
Data Visualization Training (15 Courses, 5+ Projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more