Renaming files with a plus sign in PowerShell

Viewed 895

When I've downloaded a bunch of files from dropbox, all Swedish character ä becomes +ñ. I'd like to replace this +ñ to ä.

My command is the following:

Get-ChildItem -Filter "*+ñ*" -Recurse | Rename-Item -NewName {$_.name -replace '"+ñ"','ä'}

But running this gets the follwing error message:

Rename-Item : The input to the script block for parameter 'NewName' failed. Invalid regular expression pattern: +ñ.
At line:1 char:60
+ Get-ChildItem -Filter "*+ñ*" -Recurse| Rename-Item -NewName <<<<  {$_.name -replace $str1,"ä"}
+ CategoryInfo          : InvalidArgument: (S+ñker.txt:PSObject) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.RenameItemCommand

So I've boiled it down to the + character is the problem. How do I handle + and other types of characters that isn't automatically handled in PowerShell?

1 Answers

The -replace operator does a regex search. Since + is a quantifier you have to escape it using a backslash:

Get-ChildItem -Filter "*+ñ*" -Recurse | Rename-Item -NewName {$_.name -replace '"\+ñ"','ä'}

You could also use the non regex version:

Get-ChildItem -Filter "*+ñ*" -Recurse | Rename-Item -NewName {$_.name.replace('"+ñ"','ä')}
Related