I have a bunch of .msg files in a directory and want to zip them up (to send them to our spam filtering company), but I keep getting errors on file names that contain the em dash (—). I believe it's (char)0x2014. [edited]... the correct syntax is [char]0x2014.
I wish to eliminate the em dash in the file name but I can't seem to find anything on the web about replacing em dashes. When I paste '—', PowerShell replaces it with a '-'.
I found this neat function that does a great job replacing multiple characters within a string.
http://powershell.com/cs/blogs/tobias/archive/2011/04/28/multiple-text-replacement-challenge.aspx
I am using the below code for my string replacement.
Thank you for your help.
function Replace-Text {
param(
[Parameter(Mandatory=$true)]
$text,
$replacementlist = "(-,,',,%,,$,,@,,#,,&,,’,"
)
Invoke-Expression ('$text' + -join $(
foreach($e in $replacementlist.Split(',')) {
'.Replace("{0}","{1}")' -f $e, $(
[void]$foreach.MoveNext()
$foreach.Current)
}
)
)
}$path = '<path>'
Get-ChildItem -Path $path | Rename-Item -NewName {(Replace-Text $_.Name).trim()}