Removing Illegal File Name Characters

If you have to batch-process tons of file names, you may want to make sure all file names are legal and automatically remove all illegal characters. Here is how you can do that:

$file = "this*file\\is_illegal<>.txt"
$file
$pattern = "[{0}]" -f ([Regex]::Escape([String] `
[System.IO.Path]::GetInvalidFileNameChars()))
$newfile = [Regex]::Replace($file, $pattern, '')
$newfile

Posted Jun 19 2009, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.