Forum Discussion

VidRocksKay's avatar
VidRocksKay
Copper Contributor
Aug 31, 2022
Solved

Rename folders based on a column in a CSV file using PowerSehll

Hi Everyone, 

 

I'm a beginner to PowerShell. I have a scenario where I need to rename a large amount of folders (see below) in to a new name based on a column value in CSV. 

 

Current folders naming (unfortunately these folders already exists and I couldn't think of another way to achieve this other than PS)

 

My requirement is to rename all above folders to align the SamAccount value for each user in the CSV. 

 

UPNDisplayNameSamAccount
mailto:email address removed for privacy reasonsEllen CoreyEllen.Corey
mailto:email address removed for privacy reasonsAron MichellAron.Michell
mailto:email address removed for privacy reasonsSam ColinsSam.Colins

 

What would be the best way to do this using PowerShell? 

Appreciate your support on this !! Thank you so much 

  • VidRocksKay 

    You can use this code to fix it

    $csv=Import-Csv -Path C:\boot\Book3.csv
    Foreach ($Name in $csv){
        try {
            $Filter=$Name.SamAccount.Replace('.','') +'*'
            Get-ChildItem -Directory -Path C:\boot -Filter $Filter | Rename-Item -NewName $name.SamAccount
        }
        catch {
            $_.Exception.Message
        }
    
    
    }

    replace the Path and you are good to go

4 Replies

  • farismalaeb's avatar
    farismalaeb
    Iron Contributor

    VidRocksKay 

    as I can see, the folder name is UserName with no dot with a number

    and the names in the CVS are usernames with a dot in the middle

    are all the names following the same naming convention.?

     

    • VidRocksKay's avatar
      VidRocksKay
      Copper Contributor
      farismalaeb
      Thanks for your response. Yes the naming convention is consistently the same. I basically want "FirstLast-123" to be renamed to "First.Last".
      • farismalaeb's avatar
        farismalaeb
        Iron Contributor

        VidRocksKay 

        You can use this code to fix it

        $csv=Import-Csv -Path C:\boot\Book3.csv
        Foreach ($Name in $csv){
            try {
                $Filter=$Name.SamAccount.Replace('.','') +'*'
                Get-ChildItem -Directory -Path C:\boot -Filter $Filter | Rename-Item -NewName $name.SamAccount
            }
            catch {
                $_.Exception.Message
            }
        
        
        }

        replace the Path and you are good to go

Resources