2
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.

JoeDaHobo
  • 25
  • 1
  • 1
  • 5

1 Answers1

3

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' }
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thanks, still a question, how do I figure out that the Name property outputs the file only as a string? – JoeDaHobo Sep 15 '14 at 01:52
  • 1
    You can look at the help `man Get-ChildItem -Parameter Name` or pipe the output through Get-Member e.g. `Get-ChildItem -Name *.txt | Get-Member`. – Keith Hill Sep 15 '14 at 03:20