Using Powershell 7, this works perfectly:
dir foo* | Rename-Item -Path {$_.Name} -NewName{$_.Name -replace 'bar-(.*)', 'bar-$1'}
This fails:
dir foo* | Rename-Item {$_.Name} {$_.Name -replace 'bar-(.*)', 'bar-$1'}
With the following error:
Rename-Item: A positional parameter cannot be found that accepts argument '$_.Name'.
Based on the docs here -Path
is the first position of Rename-Item
. Also, the following command works: Rename-Item 'foo-one' 'foo-one-one'
. Why does the above failure happen?