SoFunction
Updated on 2025-03-04

PowerShell implements batch renaming files

Suppose you have a large folder with pictures in it. You want to rename the names of the pictures in the folder according to a unified rule. The script in this article will be briefly demonstrated:

$i = 0
 
Get-ChildItem -Path c:\pictures -Filter *.jpg |
ForEach-Object {
$extension = $_.Extension
$newName = 'pic_{0:d6}{1}' -f $i, $extension
$i++
Rename-Item -Path $_.FullName -NewName $newName
}

All JPG files in the folder have been renamed, and the new image file name is like "pic_" and a 6-digit number. You can modify it slightly based on this template to complete richer custom rules.

Editor's note: When I was still using PadLeft and PadRight, I found that the string formatting parameters themselves were already supported.