I'm trying to automatically rename all *.txt files to *.log files, but only if they contain the upper case string ERROR.
However, the following code doesn't work:
Get-ChildItem *.txt -file | Select-String "ERROR" | Rename-Item -NewName { $_.Name -replace '.txt','.log' }
I'm getting the following error message:
Rename-Item : Cannot bind argument to parameter 'NewName' because it is an empty string.
I found out that I can get the actual path with:
Get-ChildItem *.txt -file | Select-String "ERROR" | ForEach-Object { Write-Host $_.Path }
but I can't figure out how to use it with Rename-Item
.
I also tried:
Get-ChildItem *.txt -file | Select-String "ERROR" | [io.path]::ChangeExtension($_.name, "log")
but I got the following error message:
Expressions are only allowed as the first element of a pipeline.