Removing Illegal Path Characters

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

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