Prof. Powershell

Handling Errors Before They Happen with Test-Path

The Test-Path cmdlet can keep you from going bonkers by offering a little bit of script pre-error-handling.

I always encourage people to add as much error handling as they can in their PowerShell scripts and functions. Of course you can't possibly anticipate everything that could go wrong, but you can take steps to verify or test key elements so that if there is a problem, you can handle it gracefully.

One very common feature in many PowerShell scripts and functions is referencing a file or folder. But what happens if that file or folder doesn't exist? What effect will that have on your script?

What you need to do is incorporate the Test-Path cmdlet. At its simplest, Test-Path returns True or False:

PS C:\> test-path c:\
True
PS C:\> test-path z:\foo
False

Thus in a script you might use code like this:

If (Test-Path $path) {
  Write-Host "Analyzing $path"
  #insert the rest of your code
}
Else {
  Write-Warning "Failed to find or verify $path"
}

Used in this fashion, Test-Path is validating the existence of the folder. But there may be times when you want to make sure that a value points to a potentially valid path. For that use the -IsValid parameter.

I have a folder called Work on C:\ but it does not have a child folder called Foo:

PS C:\> Test-path c:\work\foo -isvalid
True

But this is potentially a valid path in case I wanted to create it:

PS C:\> mkdir c:\work\foo

However this is false because I do not have a Z: drive:

PS C:\> Test-path Z:\work\foo -isvalid
False

You can use Test-Path to test for files as well:

PS C:\> test-path $profile
True

And you are not limited to the file system. Use Test-Path with any PSDrive:

PS C:\> test-path HKCU:\Printers\foo
False

Some PSDrives might have characters that are illegal in the file system but perfectly acceptable in another provider. This could cause a problem:

PS C:\> test-path -path 'hkcu:jdhit\[*$ecr3+]'
False

The registry key under JDHIT is perfectly legit in the registry but would fail in the filesystem. It is also a problem because PowerShell will treat some of the characters as operators or otherwise try to parse them. In these situations we'll use the -LiteralPath parameter:

PS C:\> test-path -literalpath 'hkcu:jdhit\[*$ecr3+]'
True

Now, PowerShell doesn't try to interpret anything.

Error handling is always a good thing, but look for ways to head off problems before they happen.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular