Forum Discussion

Dan_CS's avatar
Dan_CS
Copper Contributor
Aug 23, 2022
Solved

Powershell Clean up Folder Names - Rename Folders and Subfolders Recursively

    Hi friends,   I hope I have posted this in the correct forum.   I am trying to rename folders and subfolders.   I am cleaning up the folders names first removing junk spaces and things I ...
  • Harm_Veenstra's avatar
    Harm_Veenstra
    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
    }

Resources