In this post, we provide a PowerShell script that allows you to batch rename files in a specified directory based on a defined pattern. This script is particularly useful for organizing files or updating naming conventions.
powershell
$targetDirectory = 'C:\FilesToRename'
$newNamePattern = 'Report_'
$fileExtension = '.txt'
$filesToRename = Get-ChildItem -Path $targetDirectory -Filter "*$fileExtension" -File
$count = 1
foreach ($file in $filesToRename) {
$newFileName = "${newNamePattern}${count}${fileExtension}"
$newFilePath = Join-Path -Path $targetDirectory -ChildPath $newFileName
Rename-Item -Path $file.FullName -NewName $newFileName
Write-Host "Renamed '$($file.Name)' to '$newFileName'"
$count++
}
Write-Host "Batch renaming process completed. $($count - 1) files were renamed in '$targetDirectory'."Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
Ready when you are.
Try ServerEngine free for 7 days.