EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial Logical Operators in PowerShell
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

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Logical Operators in PowerShell

By Priya PedamkarPriya Pedamkar

Logical Operators in PowerShell Final

Introduction to Logical Operators in PowerShell

We have seen many conditions in if statement, like if($j -lt 10), But if we wanted to check multiple conditions at once, for example, $j -lt 10 and $i -lt 15 .So we have logical operators to tackle this kind of situation. Logical operators in PowerShell combine two or more expressions and statements together. In very simple words if we wanted to convert multiple conditions in a single condition than we can use logical operators in PowerShell. Let us call Logical Operator with the name of LO for syntax.

Syntax:

if(cond1 LO1 cond2  LO2 cond3){
Statement 1
Statement 2
...
}

So in the above syntax (cond1 LO1 cond2  LO2 cond3) combined to make one condition.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

In the above syntax (cond1 LO1 cond2 LO2 cond3)=single condition. As it will return a single value combination of multiple conditions.

Examples of Logical Operators in PowerShell

The following are the examples of logical operators in Powershell explained in detail.

Example #1

In this example we are combining three conditions all ,($a -gt $b) =one condition,(($a -lt 20) -or ($b -lt 20))=one condition by two combination and ($a -gt $b) -and (($a -lt 20) -or ($b -lt 20)) =one condition,  by a combination of all three we are getting one single condition. So if all of these conditions will be true then only output  “all combined conditions are true” will be shown.

Code:

$a =14
$b=12
if(($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
){
Write-Output “all combined conditions are true”
}

Output:

combined conditions

Example #2

In this example we are combining three conditions all ,($a -gt $b) =one condition,(($a -lt 20) -or ($b -lt 20))=one condition,($a -gt $b) -and (($a -lt 20) -or ($b -lt 20)) =one condition,  by a combination of all three we are getting one single condition. Here we can see we are combining all three conditions to form a single condition, their output will be single.

Code:

$a =20
$b=21
if(($a -gt $b) -and (($a -lt 20) -or ($b -lt 20))
){
Write-Output “all combined conditions are true”
}else{
Write-Output “all combined conditions are false”
}

Output:

Logical Operators in PowerShell 1-2

List of Logical Operators in PowerShell

There are 5 main logical operators in PowerShell, they are “and”, “or”,”xor”,”not=(!)”. let us discuss each with example in brief.

1) -and Operator

and is called as Logical and, the output of any logical and is True if $a and $b are True otherwise False, below are some examples for logical and operators.

Code:

$a -and $b //false (if both are false)
$a -and $b  //false (if any one of them is false)
$a -and $b //true (if both of them are true)

So basically logical and operator true only when both are true. The screen for the execution of the above examples is given below.

Output:

-and Operator

In general and operators used where we want all conditions should be full filled. For example, suppose In a class teacher decided to allow for exams only those students whose attendance is more than 100 and also they paid the class fee. So here both conditions need to full fill.

Code:

$attendance =101
$paid =”yes”
if($attendance -gt 100 -and $paid -eq “yes”){
Write-Output “Allow him for examination”
}

Output:

$attendance

We can also test this program by passing different inputs value of $attendance and $paid.

2) -or Operator

Logical or, False if $a and $b are False, otherwise some examples are given below:

Code:

$a -or $b //false(if both are false)
$a -or $b //true (if any one of them is true)
$a -or $b  //true (if both of them are true)

So basically logical and operator false only when both are false. The screen for the execution of the above examples is given below.

Output:

Logical Operators in PowerShell 1-5

In general or operators are used when we want to consider any condition is true like students having attended more than 100 will get 5 marks extra or the student who scores more than 200 marks.

Code:

$attendance =101
$marks =201
if($attendance -gt 100 -or $marks -gt 200){
Write-Output “give 5 marks extra”
}

Output:

Logical Operators in PowerShell 1-6

We can also test this program by passing different inputs value of $attendance and $paid.

3) -xor Operator

Logical exclusive or ,True if either $a or $b is True,otherwise

Code:

(‘a’ -eq ‘A’) -xor (‘a’ -eq ‘z’) //true as one of them is true
(‘a’ -eq ‘A’) -xor (‘Z’ -eq ‘z’)//false as one of them  is false
(‘a’ -eq ‘s’) -xor (‘Z’ -eq ‘p’) //false as both of them are false

Below screen show output of the above example,

Output:

Logical Operators in PowerShell 1-7

4) -not Operator

Logical not, True if $a is False, otherwise

Code:

-not (‘a’ -eq ‘a’)  //false as output of expression is true
-not (‘v’ -eq ‘a’)// true as output expression is false
-not (‘v’ -eq ‘V’) //false as output expression is true
-not (‘V’ -eq ‘V1’) //true as output expression is false

Screen for the above example is given below,

Output:

Logical Operators in PowerShell 1-8

5) ! Operator

The ! operator is the same as that of the -not operator. Simply! operator converts true to false and false to true.

Code:

!(‘a’ -eq ‘a’)  //false as output of expression is true
!(‘v’ -eq ‘a’)// true as output expression is false
!(‘v’ -eq ‘V’) //false as output expression is true
!(‘V’ -eq ‘V1’) //true as output expression is false

Screen for the above example is given below,

Output:

Logical Operators in PowerShell 1-9

Note: In PowerShell always use $TRUE and $FALSE for the true and false value, if you will use true and false they will not consider as a boolean value.

Code:

$a =false
$b=true
!$a //true
!($b) //true
$a=$false
$b=$true
!($a) //true
!($b) //false

Screen for the above example is given below,

Output:

Logical Operators in PowerShell 1-10

Some real-world examples with mixing all operators together,

Suppose our server and database is running and we want implements certain check were it will check all the time if server and database are running or not.

Code:

if($server -eq “running” -and $database -eq “running”){
Write-Output “server is running and database is running”
}elseif($server -eq “not running” -and $database -eq “running”){
Write-Output “server is not running and database running”
}elseif($server -eq “running” -and $database -eq “not running”){
Write-Output “server is running and database not running”
}else{
Write-Output “server and database both are not running”
}

1st Input:

$server =”not running”;
$database =”running”

2nd Input:

$server =”running”;
$database =”not running”

Conclusion

To conclude, Without a logical operator, our programming will be blank, because of logical operators only we are able to write situational code, we are able to deal with different conditions.

Recommended Articles

This is a guide to Logical Operators in PowerShell. Here we discuss the introduction and top 5 logical operators in Powershell with examples and code implementation. You may also look at the following articles to learn more –

  1. Top 9 Windows Operators
  2. Different Types of Matlab Operators
  3. Ansible vs Puppet vs Chef
  4. PowerShell Interview Questions
  5. Tutorial on Logical Operators in PHP
  6. Examples of SQL Logical Operators
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
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

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

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

EDUCBA Login

Forgot Password?

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

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

Let’s Get Started

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