The Muffin Man
The Muffin Man

Reputation: 20004

Append to file names in a loop with Powershell

Desired functionality

file1.txt ->  file1.txt.deploy
file2.txt ->  file2.txt.deploy

Attempt #82

Get-ChildItem | ForEach-Object {
  Rename-Item -NewName { $_.Name + ".deploy" }
}

Cannot evaluate parameter NewName because its argument is specified as a script block and there is no input

What am I doing wrong?

Upvotes: 1

Views: 1026

Answers (1)

JNK
JNK

Reputation: 65217

If you use ForEach-Object you need to pipe in the actual object with $_:

Get-ChildItem | ForEach-Object {
  Rename-Item $_ -NewName { $_.Name + ".deploy" }
}

Or just skip the ForEach-Object as BartekB mentions in comments:

Get-ChildItem |  Rename-Item -NewName { $_.Name + ".deploy" }

Upvotes: 2

Related Questions