Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Test-Path output -eq "False" not working

So I'm trying to check if the Path is available. I do this with the Test-Path

It looks like this:

$c="C:\"
$d="D:\"
$e="E:\"

if(Test-Path $c -eq "False"){
}
elseif(Test-Path $d -eq "False"){
}
elseif(Test-Path $e -eq "False"){
}
else{
"The File doesn't exist"
}

So what am I doing wrong if the error looks like this:

Test-Path : A parameter cannot be found that matches parameter name 'eq'.
At C:\Users\boriv\Desktop\ps\Unbenannt1.ps1:23 char:17
+ If(Test-Path $c -eq "False"){
+                 ~~~
+ CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand`
like image 600
Doggo Avatar asked Aug 22 '26 12:08

Doggo


1 Answers

You wan't to compare the result of the Test-Path cmdlet so you need to use parentheses otherwise the -eq parameter gets passed to the Test-Path cmdlet and thats the reason why you get the error.

I would use the -not operator since I found it more readable. Example:

if(-not (Test-Path $c)) {
}
like image 95
Martin Brandl Avatar answered Aug 25 '26 13:08

Martin Brandl



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!