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 Boolean
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

PowerShell Boolean

By Chirag NagarekarChirag Nagarekar

PowerShell Boolean

Definition of PowerShell Boolean

PowerShell Boolean operators are $true and $false which mentions if any condition, action or expression output is true or false and that time $true and $false output returns as respectively, and sometimes Boolean operators are also treated as the 1 means True and 0 means false.

Syntax:

Boolean type is the form of the output and its output is True or False but for the syntax, it uses the comparison and conditional operators to compare the two or multiple values.

<Value1> -eq <Value2>
<Value1> -ne <Value2>

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

And there are other syntaxes based on the commands and conditions as well. Which are shown in the examples.

How Boolean type works in PowerShell?

When evaluating the Boolean expression it compares the left side of the value to the right side of the value. If the value of the left side is equal to the value of the right side then it is evaluated true otherwise false as shown below.

"Hello" -eq "hello"

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,527 ratings)

Output:

PowerShell Boolean 1

It depends on the condition we apply. For example, the above operator is not case sensitive. If we use the case-sensitive operator then the output will be false. For example,

"Hello" -ceq "hello"

Output:

PowerShell Boolean 2

In the Boolean type, 1 is treated as $true, and 0 is treated as $false. For example,

1 -eq $true
0 -eq $false
$true -eq 1
$false -eq 0
-not 1
-not 0

Output:

PowerShell Boolean 3

In the above examples we have seen that the left side of the object is compared to the right side of the object in the -EQ operator but let evaluate the below expressions.

$true -eq 2
2 -eq $true

Output:

PowerShell Boolean 4

In the above example, the first is true but the second is false. So when the first example is true, its reverse should be also true but the Boolean operator doesn’t work that way. It works on the typecasting and the left side of the object type plays the main role here. In the first example, 2 is automatically type-casted to Boolean while in the second example, $true is type-casted to the integer as shown below.

[bool]$true -eq [bool]2
[int]2 -eq [int]$true

Output:

PowerShell Boolean 5

Condition like IF/Else also uses the Boolean output. If the condition is true then it uses the ‘IF’ block, otherwise else block is executed. For example,

if(5 -gt 6){"5 is greater than 6"}
else{"5 is less than 6"}

In the above example, if the condition checks whether the expression is true or false and that is a Boolean value and based on it, the script executes the block.

Few commands also directly return the Boolean values like Test-Path, Test-Connection, etc.

Test-Path C:\Temp\10vms.csv

Output:

output

While Test-Connection, by default doesn’t produce the Boolean output but when we add -Quiet parameter, it produces the Boolean output.

Test-Connection google123.com -Count 2 -Quiet

Output:

PowerShell Boolean 6

Dollar + QuestionMark syntax ($?) also produces the Boolean output. It checks whether the last command run was successful or failed and it gives the output accordingly.

Get-Service notexist
$?

Output:

PowerShell Boolean 7

Examples

Let us discuss examples of PowerShell Boolean.

Example #1: Using Comparison Operators to check Boolean values.

10 -eq 10
10 -gt 20
20 -gt 10
10 -lt 20
10 -le 11
10 -le 10
10 -ge 8
10 -ge 11

Output:

output 1

With String objects,

"abc" -eq "def"
"abc" -eq "abc"
"abc" -eq "abc","def"
"abc" -ne "abc"

Output:

output 2

Example #2: Using the cmdlets

Using those cmdlets which returns the output Boolean type. For example,

  • Test-Path command.

This command directly returns the Boolean value based on the path existence. For example,

Test-Path C:\Temp22
Test-Path C:\Temp

Output:

test path

  • Test-Connection command.

Some command returns the value but not the Boolean value but they support parameter which returns the Boolean value. For example, the Test-Connection command uses -Quiet parameter to return a Boolean value.

Test-Connection www.google.com -Count 2 -Quiet

Output:

test connection

Using the above command with If condition,

If(Test-Connection google.com -count 2 -Quiet -EA Ignore){
Write-Output "Connection reachable"
}
else{
Write-Output "Connection is not reachable"
}

Output:

output 3

Example #3: Commands without supported Boolean output parameter

Some commands don’t support the output which has true or the false value as the output and in that case, we can use those commands inside the IF/else condition to handle the Boolean output.

if(Get-Service WinRm -EA Ignore){
Write-Output "WINRM service is exist"
}
else{
Write-Output "WinRM service doesn't exist"
}

Output:

get service

You can also use the Where block after a pipeline to evaluate if the output is true or false. For example,

If(Get-Process | where{$_.Name -eq "Wrong Process"} -EA Ignore){
Write-Output "Process exist"
}
else{
Write-Output "Process Doesn't Exist"
}

Output:

get process

In the above example, if the process doesn’t exist, it produces the output false and executes the else condition. Otherwise, the true output and will execute the IF condition.

Example #4: Boolean type output for multiple conditions

When there are multiple conditions used, the output will be as below.

-And will check if both the conditions are true then the output is True, otherwise False.

-OR will check if any of the condition is true then the output is True, otherwise False.

(5 -lt 6) -and (4 -lt 5)
(5 -lt 6) -and (5 -lt 4)
(5 -lt 6) -or (5 -gt 4)
(5 -lt 6) -or (4 -gt 5)
(6 -lt 5) -or (4 -gt 5)

Output:

output 4

Conclusion

Boolean types (True and False) are very useful while working with the scripts. While writing scripts, programmers need to evaluate the previous output and moves to the next commands if they are true or false. It also helps to create a flow chart properly for scripts.

Recommended Articles

This is a guide to PowerShell Boolean. Here we discuss the definition, How Boolean type works in PowerShell? examples with code implementation respectively. You may also have a look at the following articles to learn more –

  1. PowerShell Wait
  2. PowerShell Invoke-Webrequest
  3. PowerShell Execution Policy
  4. PowerShell SubString
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ 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