使用Powershell在循环中附加文件名

时间:2014-07-29 02:07:47

标签: powershell powershell-v3.0

所需功能

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

尝试#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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果您使用ForEach-Object,则需要使用$_来管道实际对象:

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

或者只是跳过ForEach-Object,因为BartekB在评论中提及:

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