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

James World picture James World · Sep 6, 2012 · Viewed 12.1k times · Source

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?

Answer

JJ Willemen picture JJ Willemen · Sep 6, 2012
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)
        }