Using Test-Path to Validate A Path

by Jun 17, 2009

While raw .NET calls provides you with granular control over how to validate paths and file names, there is a cmdlet called Test-Path for simple purposes. Its primary purpose is to validate whether a file or path exists. When adding the -isValid parameter though, the cmdlet will validate the path and return $false if it found illegal characters:

do {
$path = Read-Host 'Enter a path'
} until (Test-Path -isvalid $path)