← script library

File Renaming Script

Files & FoldersJanuary 11, 2025

In this post, we present a PowerShell script that automates the process of renaming files in a specific directory based on a defined naming convention. This script is especially helpful for organizing files systematically.

powershell
$targetDirectory = 'C:\FilesToRename'
$newFilePrefix = 'Document_'
$fileExtension = '.txt'

$filesToRename = Get-ChildItem -Path $targetDirectory -Filter "*$fileExtension" -File

$count = 1
foreach ($file in $filesToRename) {
    $newFileName = "${newFilePrefix}${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 "File 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.