thotwielder thotwielder - 3 months ago 15
PowerShell Question

powershell rename all files in a folder to a pattern

I am very new to powershell. I have a folder with multiple files, each file name is like:

File1_backup_2016_09_01_095444_1628350.bak

Need to rename all of them to Filex_yyyymmdd.bak

Answer

Something similar should works for you:

Get-ChildItem "C:\temp" |foreach {
    $SplitName = $_.Name -split '_'
    Rename-Item $_.FullName -NewName "$($SplitName[0])_$($SplitName[2])$($SplitName[3])$($SplitNa‌​me[4]).bak"
}
Comments