Tag Archives: space character

Rename filenames containing strange characters with PowerShell

Recently I had a computer with tons of file on it which I had to backup and upload to OneDrive for Business (OdfB). For some unknown reason there were lots of file that had the HTML representation of a space character in it (%20), there were also filenames containing a # character.

Needless to say, the OdfB client didn’t like it, and kept complaining about not being able to upload files, it wanted to try again and again…. Renaming these files (in hundreds of directories) was not something I fancied, but this is great for PowerShell to figure out.

To find all files that contained the %20 in it I used the following command:

Dir -Recurse | Where-Object {$_.Name -like "*%20*" }

image

Now it’s just a matter of renaming the “%20” with another character, for example an underscore character “_”, like this:

Dir -Recurse | Where-Object {$_.Name -like "*%20*" } | Rename-Item -NewName { $_.Name -replace "%20","_" }

Et voila, all “%20” characters are now removed from the filenames, and ready to be uploaded to OneDrive for Business

image