Apply a new name to a folder based on its old name, read from a list

Roberto Madro R.
Roberto Madro R. used Ask the Experts™
Using Powershell, is there a way to read from a directory all the folder (folder names), save them to an Excel file, in Excel, and in an adjacent column, provide a new Folder name, and then via the script, apply the new name based onto the folder, based on its old name.

Your thoughts are appreciated.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Create the csv (will already create an empty column "NewName"):
Get-ChildItem -Path C:\Temp -Recurse -Directory | Select-Object FullName, Name, NewName | Export-Csv -Path D:\Temp\RenameDir.csv -NoTypeInformation

Open in new window

Edit away, save again as csv.
To rename the folders; in test mode so you can see what it would rename how; remove the -WhatIf at the end to run it for real.
Import-Csv -Path C:\temp\RenameDir.csv |
	Where-Object {$_.NewName} |
	Sort-Object @{e={[regex]::Matches($_.FullName, '\\').Count}} -Descending |
	ForEach-Object {Rename-Item -LiteralPath $_.FullName -NewName $_.NewName -Verbose -WhatIf}

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

oBdA assumes you want to rename subfolders too. If you only want to do for top-level folders below a folder, just remove -Recurse from the CSV generating command.
Roberto Madro R.Programmer Analyst


oBdA comes through again, saved my skin, many thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial