Forum Discussion
VidRocksKay
Aug 31, 2022Copper Contributor
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 na...
- Sep 01, 2022
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
VidRocksKay
Sep 01, 2022Copper 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".
Thanks for your response. Yes the naming convention is consistently the same. I basically want "FirstLast-123" to be renamed to "First.Last".
farismalaeb
Sep 01, 2022Iron Contributor
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
- VidRocksKaySep 01, 2022Copper Contributor
farismalaeb
This worked like charm! Thanks so much for your prompt response and support to the community !
Just to improve it a bit for everyone's benefit, could we add a line to handle duplicate ? (for instance, if the same name found in the folders, set "2 or 3" at the end).
like - JonDoe-123 , JonDoe-456
Renamed as JonDoe, JonDoe2
Again, thanks very much !