Elseif is unrecognized

Hello!

I’m an absolute newbie (two weeks into learning PS) and I’ve hit a speedbump with the following code:

$path = 'C:\Test'

$evalPath = Test-Path $path

if ($evalPath -eq $true) {

    Write-Host "$path VERIFIED"

}

elseif ($evalPath -eq $false) {

    Write-Host "$path NOT VERIFIED"

}

I’m getting this error in the console when I run the above:

elseif: The term 'elseif' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I’m unsure why I’m getting this error, as “if” is recognized. And I don’t think I have any open brackets (which seems to be a common cause to this issue in my research.) I’m using VS Code with PS v7.2.2

Thanks in advance!

LwithaZ,
Welcome to the forum. :wave:t4:

Simply move the elseif behind the closing curly brace of the if script block. :wink:

But your code is a bit clunky anyway. Since the output of Test-Path is a [boolean] you can use it directly in the if statement. So it should look soemthing like this:

$path = 'C:\Test'
if (Test-Path -Path $path) {
    "$path VERIFIED"
}else {
    "$path NOT VERIFIED"
}
1 Like

Like magic, thank you! And thanks for the cleanup of my script. I’m just getting my hands dirty with this stuff, so I appreciate knowing good habits early.

Then I have a reading recommendation for you

1 Like

Oh hell yes! This is fantastic