Question:
Basically I want to do a check if a directory exists then run this section, if not exit.
The script I have is:
1 2 3 4 5 6 7 8 9 10 11 |
$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.
Answer:
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,
1 2 3 4 5 6 7 8 9 10 11 |
$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,
1 2 3 4 5 6 7 8 9 10 11 |
$Path = Test-Path c:\temp\First if (!$Path) { Write-Host "notthere" -ForegroundColor Yellow } elseif ($Path) { Write-Host " what the smokes" } |