Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True/false test returning unexpected results

Basically I want to do a check if a directory exists then run this section, if not exit.

The script I have is:

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

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

But it returns nothing.

like image 983
Norrin Rad Avatar asked Jun 07 '26 12:06

Norrin Rad


1 Answers

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"
}
like image 107
scharette Avatar answered Jun 10 '26 17:06

scharette



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!