Using Test-Path to Validate A Path

Share |

Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus

13 Free SQL Tools from Idera

Free Ebook: Mastering Powershell

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 All rights reserved.