powershellget-childitemrename-item-cmdlet

In powershell, how can I use Rename-Item to output the old and new file names at the same time?


I'd like to select a list of files using Get-ChildItem piped to Rename-Item and have the output display text with each line showing something like "Renamed oldfilename to newfilename".

How can I do this?


Solution

  • Get-Childitem C:\Temp |
        Foreach-object {
            $OldName = $_.name; 
            $NewName = $_.name -replace 'temp','test'; 
            Rename-Item -Newname $NewName; 
            Write-Output $("Renamed {0} to {1}" -f $OldName,$NewName)
            }