Help Creating a Script to Find and Rename Folders/Files Containing the Ampersand Character

homerslmpson used Ask the Experts™
I recently started messing with SharePoint Foundation 2013 here at work.  I started copying some folders/files over and almost immediately ran into an issue with folders/files throwing errors due to there being illegal characters in the folder/file names.

I'm now looking for a way to rename any and all folders/files that contain an ampersand in the name.

I'm hoping someone here could help me with creating a script for this.  I'm thinking it would need to consist of 2 "parts" due to some folder/file names having a space before/after the ampersand and other folder/file names having no space before/after the ampersand.

Essentially, I'm looking to have a script created that can do the following on a Windows Server OS:
- search a folder and all sub-folders for any and all folders and filenames containing space&space <-- example file/folder name Alligators & Crocodiles
- rename these folders/files spaceandspace <-- example file/folder name Alligators and Crocodiles

- search the same series of folders for any and all folders and files containing no space&no space <-- example file/folder name Up&Down
- rename these folders/files spaceandspace <-- example file/folder name Up and Down

I'd appreciate any help you could provide me with.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
PowerShell; adjust the $baseDir variable, save as Whatever.ps1, and run with an account with enough permissions on the file server.
If PowerShell complains about the script not being signed, run
Set-ExecutionPolicy Bypass -Force
The script is in test mode and will only show what it would rename how. Set the variable $testMode to $false to run it for real.
It will do the renaming including surrounding spaces in one go.
$baseDir = 'C:\Test'
$testMode = $true
$verbose = $true

$pattern = '\s*&\s*'
$replaceWith = ' and '
Get-ChildItem -Path $baseDir -File -Recurse |
	Where-Object {$_.Name -match $pattern} |
	Rename-Item -NewName {$_.Name -replace $pattern, $replaceWith} -Verbose:$verbose -WhatIf:$testMode
Get-ChildItem -Path $baseDir -Directory -Recurse |
	Where-Object {$_.Name -match $pattern} |
	Sort-Object -Property @{e={$_.FullName.Split('\').Count}} -Descending |
	Rename-Item -NewName {$_.Name -replace $pattern, $replaceWith} -Verbose:$verbose -WhatIf:$testMode

Open in new window


Thank you so much!
It's perfect!
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

For these one time type renaming activities I often use a utility program rather than a script.  They allow you to preview the changes first and have lots of options and flexibility.  There is a learning curve, but once you get the hang of them the next times go faster...

A couple that I keep in my toolbox:


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial