Renames files using a base name and incremented number – PowerShell

Video demo

Link: Rename all files using a base name and incremented number – YouTube

You can copy the script from this page and modify according to your needs.

PS script

This is the PowerShell script I wrote to rename files.


# This script renames all files in a specified folder with a new base name
# and a consecutive, zero-padded number (e.g., "my_file_001.txt").

# --- USER-DEFINED VARIABLES ---
# Specify the path to the folder containing the files you want to rename.
# Example: C:\Users\YourUsername\Documents\Photos
$folderPath = "C:\Users\administrator\Desktop\Files"

# Specify the new base name for the files.
# Example: "sanuja" will result in names like "sanuja_001.jpg"
$newName = Read-Host -Prompt "Please specify the new file base name: "

# Specify the starting number for the sequence.
# This will be formatted to be zero-padded.
$startNumber = Read-Host -Prompt "Specify the starting number for the sequence: "
$startNumber = [int]$startNumber

# --- SCRIPT LOGIC ---
# Get all files in the specified folder.
# The `-File` switch ensures we only get files and not subdirectories.
$files = Get-ChildItem -Path $folderPath -File

# Check if any files were found.
if ($files.Count -eq 0) {
Write-Host "No files found in the specified folder: $folderPath"
} else {
Write-Host "Found $($files.Count) files. Renaming..."

# Loop through each file found in the folder.
foreach ($file in $files) {
# Create a new, zero-padded number string (e.g., 1 becomes "001").
$paddedNumber = "{0:D3}" -f $startNumber

# Construct the new file name using the base name, padded number, and the original extension.
# Example: "sanuja_001.jpg"
$newFileName = "${newName}_${paddedNumber}$($file.Extension)"

# Define the full path for the new file.
$newFilePath = Join-Path -Path $folderPath -ChildPath $newFileName

# Rename the file. The `-WhatIf` parameter is commented out.
# UNCOMMENT `-WhatIf` to see what the script will do without making any changes.
Rename-Item -Path $file.FullName -NewName $newFileName #-WhatIf

# Write a message to the console to show the progress.
Write-Host "Renamed $($file.Name) to $newFileName"

# Increment the counter for the next file.
$startNumber++
}

Write-Host "File renaming complete."
}