Alex Kwitny
Alex Kwitny

Reputation: 11544

Test-Path is erroring instead of returning True/False

This is very straight forward. I'm writing a powershell function and I want to validate if a parameter is a valid directory, but instead of returning True or False, Test-Path is erroring?

This is counter to what I'm reading about the behavior of the function.

if (Test-Path -PathType Container "asdf")
{
    Write-Output "Exists"
}
else
{
    Write-Output "Does not exist"
}

enter image description here

Upvotes: 0

Views: 216

Answers (3)

briantist
briantist

Reputation: 47772

The answer you accepted is right, but I wanted to expand on what you can do differently.

This was a problem up until very recently. PowerShell's SQL server support has been vastly improved, so much so that they created a new module for it, so if you're still using SQLPS look into using the SqlServer module instead.

With SQLPS, it automatically changes the drive to the SQLSERVER: provider.

If you must continue using or supporting SQLPS and want to avoid this behavior (it can also make tab completing super slow), I'm fond of this pattern:

Push-Location
Import-Module SQLPS
Pop-Location

This saves your current location in the location stack, imports the module (which will change your location), then pops your previous location back off, so you're back where you started.

I strongly encourage you to look at using the new module as it has some really important enhancements and improvements.

Upvotes: 2

cet51
cet51

Reputation: 1226

You're set up in the SQLSERVER drive. You need to be back in your local file system. Go ahead and do a

Set-Location C:

Then you will be able to run your If statement.

I would have commented this onto the first answer that was given to clarify how to get to the local file system, but my low reputation restricts me from commenting.

Upvotes: 0

Shawn Esterman
Shawn Esterman

Reputation: 2342

You're in the SQLSERVER drive. You need to change your location back to a FileSystem drive.

Upvotes: 1

Related Questions