I have a simple PowerShell script that bulk renames files and I'd like to have a table output that shows 'Old File Name' and 'New File Name' to show the changes. How do I access/reference the new file name while still inside the 'ForEach-Object' loop, assuming it can be done? Can I refresh that particular value of $_? I don't want to just use my variable $newname because I created that and since this is supposed to be output that shows the file names were actually changed, I'd like to access the file's new name from the system as determined by the script.
Write-Host "Old Name `t`t`t`t`t`t New Name"
Write-Host "-------- `t`t`t`t`t`t --------"
$files = Get-ChildItem -Path C:\Users\<user>\Desktop\Config
$files | % ({
if ((!$_.PsIsContainer) -and ($_.Extension -eq '')) {
$oldname = $_.FullName
$newname = "$($_.FullName).cfg"
Rename-Item $oldname $newname
# $_.Name still refers to the old file name without the extension
# How do I immediately access its new name (including extension)?
Write-Host $_.Name `t`t`t`t`t`t $newname
}
})