Search code examples
powershellpowershell-2.0rename-item-cmdlet

Cannot Bind Argument to Parameter 'NewName' because it is an empty string


Get-ChildItem -Name *.txt | Rename-Item -NewName { $_.name -replace '\.txt','.log' }

I have 3 text files in my current path, I'm using this snip bit of code found in the last example of...

get-help rename-item -full

(Powershell Version 2.0). For whatever reason, I keep receiving the following error:

Rename-Item : Cannot bind argument to parameter 'NewName' because it is an empty string.
At line:1 char:40
+ Get-ChildItem -Name *.txt | Rename-Item <<<<  -NewName { $_.name -replace 
'\.txt','.log' }
+ CategoryInfo          : InvalidData: (testfile3.txt:PSObject) [Rename-Item], 
ParameterBindingValidationException
+ FullyQualifiedErrorId : 
ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.Rena
meItemCommand

Clearly my alteration form .txt to .log isn't an empty string, and this matches exactly the same code as in found in Microsoft's last example of the cmdlet rename-item.


Solution

  • Either don't use the -Name parameter since that outputs just strings containing the full path or don't reference the Name property:

    Get-ChildItem -Name *.txt | Rename-Item -NewName { $_ -replace '\.txt','.log' }