GoldenAge
GoldenAge

Reputation: 3068

Rename all folders with given name in a directory using powershell

I want to find all folders in the directory by name e.g. Help and rename them to blah. Ive tried this:

Get-ChildItem -Path 'C:\dev\foo' -Filter 'Help' -Recurse | ForEach-Object {
    Rename-Item -Path $_.FullName -NewName 'blah'
}

but it doesn't seem to work. Any ideas? Cheers

Upvotes: 1

Views: 682

Answers (1)

Bakudan
Bakudan

Reputation: 19482

$path = "C:\dev\foo"
$oldName = "Help"
$newName = "blah"

Get-ChildItem -Path $path -Filter "*$oldName*" -Recurse | 
    Rename-Item -NewName { $_.name -Replace $oldName, $newName } -WhatIf

You were missing the wild card - "*$oldName*". This way you are searching for all folders containing Help in the name, not just the one named Help. The -WhatIf parameter will show you all of the folders that will be renamed without actually renaming them. Remove it when you check if the result will be correct.

Upvotes: 3

Related Questions