EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Boolean
 

PowerShell Boolean

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated March 4, 2023

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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>

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"

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW