Using Test-Path to Validate A Path

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

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)

Posted Jun 17 2009, 08:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.