Removing Illegal Path Characters

by Jun 18, 2009

You can always strip all illegal characters from the path If you have no time to review path names and correct them manually to see if they contained illegal characters. Here is how you can do that:

$path = "c:<>illegal path"
$path
$pattern = "[{0}]" -f ([Regex]::Escape([String] `
[System.IO.Path]::GetInvalidPathChars()))
$newpath = [Regex]::Replace($path, $pattern, '')
$newpath