Forum Discussion
Powershell Clean up Folder Names - Rename Folders and Subfolders Recursively
- Aug 23, 2022
Dan_CS It's the special characters, if you replace @ with at for example, it does work for me
$Source_Folder = 'd:\temp\Test' $folders = Get-ChildItem -LiteralPath $Source_Folder -Directory Foreach ($folder in $folders) { $new_folder_name = $folder.Name # Remove Characters $new_folder_name = $new_folder_name -replace '#', ' ' # Remove Hyphen with _ $new_folder_name = $new_folder_name -replace '-', '_' # Remove @ with at $new_folder_name = $new_folder_name -replace '@', 'at' # and some more replacements...... Write-Host "Renaming:" $folder.FullName "to" $new_folder_name Rename-Item "$($folder.FullName)" "$($new_folder_name)" -Verbose }
My folder structure.
I wanted to clean up the folder names
Dan_CS It's the special characters, if you replace @ with at for example, it does work for me
$Source_Folder = 'd:\temp\Test'
$folders = Get-ChildItem -LiteralPath $Source_Folder -Directory
Foreach ($folder in $folders) {
$new_folder_name = $folder.Name
# Remove Characters
$new_folder_name = $new_folder_name -replace '#', ' '
# Remove Hyphen with _
$new_folder_name = $new_folder_name -replace '-', '_'
# Remove @ with at
$new_folder_name = $new_folder_name -replace '@', 'at'
# and some more replacements......
Write-Host "Renaming:" $folder.FullName "to" $new_folder_name
Rename-Item "$($folder.FullName)" "$($new_folder_name)" -Verbose
}- Dan_CSAug 23, 2022Copper Contributor
This powershell is very exhausting - I try to avoid using it for this exact reason, its not for simple folks.
But I had 5000 files and bulk rename utility takes so long to use so I thought powershell.
What I learned from this
1. Open powershell as Administrator > to avoid Access denied on some folders
2. It doesn’t like special characters like @, #I will try to figure out how to replace whitespace, it doesnt like replacing whitepsace like this ? " " , ""
Again thank you for your help and you are a STAR!
Have a great day 🙂
Dan
- NevsDnulMay 30, 2023Copper Contributor
Dan_CS
There is only one problem with this script if you have a folder named
10. Folder name, and you need to remove the .
it removes not only the . but it also removes the 0
So i tried some tweaks without finding a solution
setting a space in the unwanted character position ignored the 10. completly
and get childitem ignored the subfolders aswell.
Cool script but it does not do what i want. - Aug 23, 2022No problem 🙂