PowerShell–Rename the ‘&’ in File Names

So recently I was moving large amounts of .DOC files into SharePoint libraries and it was occasionally causing an error due to invalid characters in the file name.  This was mostly due to having abbreviations in the names that included ampersands, for example “M&A Report 2010.doc”.  Now, looking at this, it’s an easy fix to replace the ‘&’ character with an underscore with PowerShell, this example does it for all files in a directory:

get-childitem *.* | foreach { rename-item $_ $_.Name.Replace("&", "_") }

Which works fine for the files within that folder, what if you want to do it recursively and include the subfolders?  no problem for PowerShell thanks to the ‘-recurse” flag:

get-childitem *.* -recurse | foreach { rename-item $_ $_.Name.Replace("&", "_") }

And there you have it, an easy and simple way to prep a file share or set of directories to move into a SharePoint library.

Leave a Reply