1

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?

3
  • change {} with () in -newname parameter value. Commented Jul 29, 2014 at 5:54
  • 1
    Or pipe directly to Rename-Item and skip Foreach-Object entirely. It's not necessary in this case. Commented Jul 29, 2014 at 7:32
  • Why would you use {} over ()? I know the first is a script block, but why is it not appropriate in this case? Commented Jul 29, 2014 at 17:06

1 Answer 1

2

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" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.