pooja520  PowerShell is not strongly-typed language like C#, where the compiler wouldn't even let you run a test like [bool] -eq [string]. Where PowerShell does allow such operations, they come with behaviours you need to know about. What your first test ($Stat -eq 'fal') is testing is for the existence (i.e. present and not null) of the right of the operator - since the types themselves don't match - and comparing that to the value on the left. So, rather than: $Stat -eq 'fal'; You're actually comparing (since 'fal' both exists and is not null): $Stat -eq $true; Hence the result is $true. Here's my own example illustrating this very point using an even more complex type on the right-hand side:  Now, if you swap the values around, you'd expect to get the same outcome, but you don't. This is because PowerShell is now testing for equality against the complex ActiveDirectorySite class, which the Boolean isn't going to match (since the test is something called a reference equality test - but this isn't important.) So, this brings me to the crux of your issue: how can you reliably test a Boolean against another Boolean? (as distinct from your example that is a Boolean against a String.) I'd posit two basic ways though there are more: Swap the Boolean to be on the right side of the operator, with the object you're comparing to the left;Use the .NET .Equals() method on the Boolean object to assess the object being checked.  PowerShell's implicit existence testing can be quite a handy feature, but in the case of working with the Boolean type also requires a bit more care - and testing - to avoid unintended outcomes. Cheers,Lain Answer from lainrobertson on techcommunity.microsoft.com
Top answer
1 of 1
2
pooja520  PowerShell is not strongly-typed language like C#, where the compiler wouldn't even let you run a test like [bool] -eq [string]. Where PowerShell does allow such operations, they come with behaviours you need to know about. What your first test ($Stat -eq 'fal') is testing is for the existence (i.e. present and not null) of the right of the operator - since the types themselves don't match - and comparing that to the value on the left. So, rather than: $Stat -eq 'fal'; You're actually comparing (since 'fal' both exists and is not null): $Stat -eq $true; Hence the result is $true. Here's my own example illustrating this very point using an even more complex type on the right-hand side:  Now, if you swap the values around, you'd expect to get the same outcome, but you don't. This is because PowerShell is now testing for equality against the complex ActiveDirectorySite class, which the Boolean isn't going to match (since the test is something called a reference equality test - but this isn't important.) So, this brings me to the crux of your issue: how can you reliably test a Boolean against another Boolean? (as distinct from your example that is a Boolean against a String.) I'd posit two basic ways though there are more: Swap the Boolean to be on the right side of the operator, with the object you're comparing to the left;Use the .NET .Equals() method on the Boolean object to assess the object being checked.  PowerShell's implicit existence testing can be quite a handy feature, but in the case of working with the Boolean type also requires a bit more care - and testing - to avoid unintended outcomes. Cheers,Lain
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › scripting › learn › deep-dives › everything-about-if
Everything you wanted to know about the if statement - PowerShell | Microsoft Learn
We can use normal PowerShell inside the condition statement. ... Test-Path returns $true or $false when it executes. This also applies to commands that return other values. ... It evaluates to $true if there's a returned process and $false if ...
Top answer
1 of 4
17

The error comes from the fact that the return value of Test-Path is a Boolean type.

Hence, don't compare it to strings representation of Boolean but rather to the actual $false/$true values. Like so,

$Path = Test-Path  c:\temp\First

if ($Path -eq $false)
{
    Write-Host "notthere" -ForegroundColor Yellow
}
elseif ($Path -eq $true)
{
    Write-Host " what the smokes"
}

Also, note that here you could use an else statement here.

Alternatively, you could use the syntax proposed in @user9569124 answer,

$Path = Test-Path  c:\temp\First

if (!$Path)
{
    Write-Host "notthere" -ForegroundColor Yellow
}
elseif ($Path)
{
    Write-Host " what the smokes"
}
2 of 4
5

In a comparison operation PowerShell automatically converts the second operand to the type of the first operand. Since you're comparing a boolean value to a string, the string will be cast to a boolean value. Empty strings will be cast to $false and non-empty strings will be cast to $true. Jeffrey Snover wrote an article "Boolean Values and Operators" about these automatic conversions that you can check for further details.

As a result this behavior has the (seemingly paradox) effect that each of your comparisons will evaluate to the value of your variable:

PS C:\> $false -eq 'False'
False
PS C:\> $false -eq 'True'
False
PS C:\> $true -eq 'False'
True
PS C:\> $true -eq 'True'
True

Essentially that means that if your Test-Path statements evaluates to $false neither of your conditions will match.

As others have pointed out you can fix the issue by comparing your variable to actual boolean values, or by just using the variable by itself (since it already contains a boolean value that can be evaluated directly). However, you need to be careful with the latter approach. In this case it won't make a difference, but in other situations automatic conversion of different values to the same boolean value might not be the desired behavior. For instance, $null, 0, empty string and empty array are all interpreted as a boolean value $false, but can have quite different semantics depending on the logic in your code.

Also, there is no need to store the result of Test-Path in a variable first. You can put the expression directly into the condition. And since there are only two possible values (a file/folder either exists or doesn't exist), there is no need to compare twice, so your code could be reduced to something like this:

if (Test-Path 'C:\temp\First') {
    Write-Host 'what the smokes'
} else {
    Write-Host 'notthere' -ForegroundColor Yellow
}
🌐
PDQ
pdq.com › blog › how-to-use-if-statements-in-powershell
How to use if statements in PowerShell | PDQ
PowerShell checks the expression in parentheses, and if it returns true, it executes the statements inside the braces. If it’s false, PowerShell skips over that block and continues to the next step.
🌐
Reddit
reddit.com › r/powershell › strange way to write an if-check on a boolean
r/PowerShell on Reddit: Strange way to write an if-check on a boolean
September 18, 2023 -

Source: https://github.com/Esri/arcgis-powershell-dsc/blob/main/Modules/ArcGIS/ArcGIS.psm1

Lines 1953 and 1973 specifically.

I am looking at some powershell code (link above), and don't understand why a simple boolean check is written as:

$JobFlag = $True
# more code here...
if ($JobFlag[$JobFlag.Count - 1] -eq $True) { # CODE HERE }

There are some functions that might be called before the if-statement, but as far as I can tell, they return either $True or $False

Is there any reason for not writing a plain:

if ($JobFlag -eq $True)

I am by now means an experienced powershell user, therefore curious.

🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_if
about_If - PowerShell | Microsoft Learn
January 16, 2025 - If <test1> is true, <statement list 1> runs, and PowerShell exits the if statement. If <test1> is false, PowerShell evaluates the condition specified by the <test2> conditional statement.
Find elsewhere
🌐
PowerShell Forums
forums.powershell.org › powershell help
$true or $false in powershell function - PowerShell Help - PowerShell Forums
December 14, 2020 - I came across one script and I’m puzzled about one simple function in a script. To my understanding, result should be always $false as it has return $false at the bottom of the script or I’m missing something? Can someone explains the logic here. Thank you #This function checks to see if the file should be ignored.
🌐
Powershellexplained
powershellexplained.com › 2019-08-11-Powershell-if-then-else-equals-operator
Powershell: Everything you wanted to know about the IF statement
August 11, 2019 - If the value was $false, then it would skip over that scriptblock. In the previous example, the if statement was just evaluating the $condition variable. It was $true and would have executed the Write-Output command inside the scriptblock.
🌐
Xah Lee
xahlee.info › powershell › powershell_boolean.html
PowerShell: True, False (boolean)
May 26, 2021 - PowerShell: True, False (boolean) PowerShell: Boolean Operators · PowerShell: If Then Else (Flow Control) PowerShell: Test Equality · PowerShell: Test Order (Greater, Lesser) PowerShell: Check Type ·
🌐
EDUCBA
educba.com › home › data science › data science tutorials › powershell tutorial › powershell boolean
PowerShell Boolean | How Boolean type works in PowerShell?
March 4, 2023 - 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 ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
LazyAdmin
lazyadmin.nl › home › how to use powershell if else statements
How to use PowerShell If Else Statements — LazyAdmin
May 9, 2023 - The PowerShell If statement checks if a particular condition is true, but sometimes you only want to check if a condition is false. For this, we can use the If Not statement in PowerShell. This will check if a condition is not met.
🌐
TutorialsPoint
tutorialspoint.com › powershell › if_else_statement_in_powershell.htm
Powershell - If Else Statement
if(Boolean_expression) { // Executes when the Boolean expression is true }else { // Executes when the Boolean expression is false }
🌐
MorganTechSpace
morgantechspace.com › home › bool value check with if statement and where object in powershell
Bool Value Check with IF Statement and Where Object in PowerShell
March 5, 2020 - $Result=@() 1..25 | ForEach-Object { $Result += New-Object PSObject -property @{ ID = $_ Status = if (-not($_ % 2)){$true} else {$false} }} // Example 1: $Result | Where {$_.Status -eq $true} // Example 2: $Result | Where {$_.Status} // Example 3: Inverse boolean check $Result | Where {-not ($_.Status)}
🌐
Petri
petri.com › home › boolean values in powershell
Boolean Values in PowerShell
September 4, 2024 - Boolean values in Windows PowerShell. (Image Credit: Jeff Hicks) One thing I want to point out is that often $True and $False can be implicit. By that I mean, if something exists it can be said to have a value of $True. I realize there are some developers who might take exception to my ...
🌐
Adam the Automator
adamtheautomator.com › powershell-if-statement
Back to Basics: Conditional Logic with PowerShell If Statement
If the result is true, the result saying "$num is greater than 10" will be displayed on the screen. If the result is false, PowerShell does nothing because there is only one condition to test.
Published   May 25, 2023
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_logical_operators
about_Logical_Operators - PowerShell | Microsoft Learn
January 7, 2026 - ... Logical OR (-or) - TRUE when either statement is TRUE. ... Logical NOT (-not) or (!) - Negates the statement that follows. -not (1 -eq 1) # Result is False !(1 -eq 1) # Result is False
🌐
AttuneOps
attuneops.io › powershell-if-else-and-elseif
Introduction to PowerShell If, Else, and ElseIf Statements - AttuneOps
September 30, 2025 - It condenses the code, making it more readable and easier to maintain than if you had a long string of if-elseif statements. Yes, with PowerShell 7 and later, you can use a ternary operator. The syntax is: $result = $condition ? ‘TrueValue’: ‘FalseValue’
🌐
Mike Frobbins
mikefrobbins.com › 2022 › 06 › 09 › using-the-conditional-ternary-operator-for-simplified-if-else-syntax-in-powershell-7
Using the conditional ternary operator for simplified if/else syntax in PowerShell 7 | mikefrobbins.com
June 9, 2022 - It returns false if no resources exist in the resource group. 1$resourceGroup = '<resource-group-name>' 2(Get-AzResource -ResourceGroupName $resourceGroup) ? $true : $false · Variables set inside the parentheses can be used for logic in the ...
🌐
Microsoft Certified Professional Magazine
mcpmag.com › articles › 2016 › 03 › 09 › working-with-the-if-statement.aspx
PowerShell Basics: Working with the If Statement -- Microsoft Certified Professional Magazine Online
March 9, 2016 - How this works is that you take your item that you want to compare against another item and use a comparison operator such as –eq (this means that you want to see if something is equal to whatever you are comparing it against; more on these later) which then returns a Boolean value of True (meets the comparison) or False (does not meet the comparison).