Search code examples
powershellrename

Change the Name of a list of folders


I have a list of folders on UNC Path in a CSV file the entries look like this \Server\share\sales\John I want to rename the folder John so it looks like Migrated_John but I need to do this with many different paths and the script needs to be used many times. so I dont want to add a column wiht new_Name for each entry can you please help with this

I have wrote this so far but I dont seem to be able to do it right

$folders = Import-Csv -Path "C:\temp\FolderList.csv"

foreach ($folder in $folders) {
    $oldName = $folder.Path
    $newName = "Migrated_" +
    Rename-Item $oldName $newName
}

What should go after the +


Solution

  • First try to get the parameters of the $oldName with:

    $folders = Import-Csv -Path "C:\temp\FolderList.csv"
    
    foreach ($folder in $folders) {
        $oldName = $folder.Path
        $oldNameParams = Get-Item $oldName
        $newName = "Migrated_" + $oldNameParams.PSChildName
        Rename-Item $oldName $newName
    }