1

I have to rename literally thousands of folders and want to use a script which adds "Old" in front of the old Name.

I tried this script

Get-ChildItem | rename-item -NewName { "Old+ $_.Name }

It even works when I try it in some small test folders to prevent messing up my main folder but unfortunally it's working there but not in my main folder. In my main folder this command will loop until it hits the character limit and stops.

Looks like this:

Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Old Zimmer.

Online i found this one too but it produces the same result

get-childitem | % { rename-item $_ "Old $_"}

Is it a bug or am I just stupid?

Theo
  • 57,719
  • 8
  • 24
  • 41
weda
  • 11
  • 2
  • 1
    `Get-ChildItem | rename-item -NewName { "Old_$_" } -WhatIf` should work. Use `-WhatIf` parameter to see the changes before they are actually made. – Vivek Kumar Singh Dec 19 '18 at 14:22
  • 1
    Use directory snapshot. `$items = Get-ChildItem; $items | rename-item -NewName { "Old+ $_.Name }`. Magic code: Iterators. – Paweł Dyl Dec 19 '18 at 14:22
  • 2
    your first attempt has an unclosed quote. Is that a copy paste error? What was the exact code you used. How many files are in the new folder. – Matt Dec 19 '18 at 14:25

1 Answers1

1

To rename the folders in a given path by prefixing them with "Old", this works for me:

$path = "<PATH TO YOUR MAIN FOLDER>"
Get-ChildItem -Path $path -Directory | Rename-Item -NewName { "Old$($_.Name)" }

# For PowerShell version less than 3.0
# Get-ChildItem -Path $path | Where-Object { $_.PSIsContainer} | Rename-Item -NewName { "Old$($_.Name)" }

(by not setting the -Path parameter, the Get-ChildItem cmdlet uses the default location which is the current directory . or $pwd)

Theo
  • 57,719
  • 8
  • 24
  • 41