Share via


I need a powershell command to replace unicode chars in a file name to a blank space

Question

Tuesday, September 16, 2014 9:21 AM

Can anyone provide me with a powershell command which will search and replace a specific unicode character in a file name to just a blank space?

Thanks

All replies (7)

Wednesday, September 24, 2014 8:48 AM ✅Answered

Hi Roadster,

Please check the text files filtering by the file name:

Get-ChildItem -Path c:\test\*.txt -Recurse | where{$_.name -like "*u2122*"} 

Then rename the files resulting from above script with the script below:

Get-ChildItem -Path c:\test\*.txt -Recurse | where{$_.name -like "*u2122*"} | rename-item -newname {$_.name -replace "u2122","1"}

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang


Tuesday, September 16, 2014 12:20 PM | 1 vote

You should be able to use the -replace operator:

"C:\temp\myфfilename.txt" -replace "ф", " "

Or, if you know the unicode character code for the character you wish to remove:

# 002C hex is 44 decimal = comma ","
"C:\temp\my,filename.txt" -replace "\u002C", " "

Best Regards,

Carl S

All code is provided as-is with no guarantees. Always try it out in a test environment before applying it in a production environment.


Wednesday, September 17, 2014 6:51 AM

Hi Roadster198,

In addition, if you want to search and rename the text files under a folder, please also check this script:

Get-ChildItem -Path d:\test1\1\*.txt -Recurse| where{$_.name -like "*ф*"}|rename-item -newname { $_.name -replace "ф"," " }

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

TechNet Community Support
 


Thursday, September 18, 2014 7:47 AM

Thank you all for your assistance with my issue. I've managed to resolve it. I'll get the hang of this powershell if it kills me :)


Monday, September 22, 2014 10:32 AM

Hi Anna,

              I've changed your command slightly.

Get-ChildItem -Path c:\test\.txt -Recurse | where{$_.name -like "*u2122*"} | rename-item -newname {$_.nAme -replace "u2122","1"}

However this doesn't do anything to any of the files in the C:\test folder.


Wednesday, September 24, 2014 8:58 AM

Hi Anna, thank you for your help. Rookie mistake as I left out a space in the command which is why it didn't work. Thank you for all you help.


Wednesday, September 24, 2014 9:00 AM

Hi Roadster,

Glad to hear that =)