Although your example code does not actually rename any file (-replace 'bar-(.*)', 'bar-$1'
will result in the exact same name.), the main problem here is that you add {$_.Name}
directly after the Rename-Item
cmdlet, where this is not needed, since you are already piping fileInfo objects through the pipeline, so now {$_.Name}
is treated as if it were a parameter name.
Next, you forget to add the name for parameter -NewName
.
By iterating through a path like foo*
you do the same as -Path 'foo' -Recurse
and there's a gotcha in that..
Especially when iterating through many files and passing them on through the pipeline, there is a chance that files that have already been renamed get picked up by the Get-ChildItem
(alias dir
) again and then will be renamed twice.
To overcome that, put the part to gather the files between brackets, so collecting all the FileInfo objects is complete and then pipe that to the next cmdlet like so:
(Get-ChildItem -Path 'foo' -File -Recurse) |
Rename-Item -NewName {$_.Name -replace 'bar-(.*)', 'baz-$1'}
for clarity, this does change part of the name from bar
to baz