April 9th, 2015

PowerTip: Use PowerShell to Verify Syntax of Path

Doctor Scripto
Scripter

Summary: Learn how to use Windows PowerShell to verify the syntax of a path.

Hey, Scripting Guy! Question How can I use Windows PowerShell to verify that a path that accepts user input is using the correct syntax?

Hey, Scripting Guy! Answer Use the –IsValid parameter of the Test-Path cmdlet, for example:

PS C:\> $a = "c:\myfolder\nonexistentfile.txt"

PS C:\> Test-Path $a -IsValid

True

PS C:\> $b = "c:\my()(*&^%\afile.txt"

PS C:\> Test-Path $b -IsValid

False

Note  This command verifies if the syntax is correct for the provider, but it does not verify if the file exists.

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.