I'm looking for an explanation on why parameter binding fails when using script blocks without specifying named parameters in a piped Rename-Item
cmdlet.
Why does this work:
dir file.txt | ren -path {$_.name} -newname {$_.name -replace 'txt','doc'}
but this does not work:
dir file.txt | ren {$_.name} {$_.name -replace 'txt','doc'}
It returns this error:
Rename-Item : A positional parameter cannot be found that accepts argument '$_.name'.
At line:1 char:19
+ dir file.txt | ren <<<< {$_.name} {$_.name -replace 'txt','doc'}
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RenameItemCommand
The Rename-Item
syntax seems pretty straightforward to me (ie. two mandatory positional parameters):
SYNTAX
Rename-Item [-Path] <string> [-NewName] <string> [-Credential <PSCredential>] [-Force] [-PassThru] [-Confirm] [-WhatIf] [-UseTransaction] [<CommonParameters>]