PowerShell: Bulk Rename Files by Date or Number With a One-Line Undo

Advertisement
File Operations
Advertisement

Camera files like IMG_0001.jpg, scanned invoices and meeting notes: sooner or later you want a whole folder of them named in one consistent pattern, such as 20261101_001.jpg. Changing hundreds of names by hand is not realistic.

The short answer: PowerShell can change them all at once. I recommend saving the old and new names in a CSV first. If you get it wrong, a single line puts everything back. The script below starts in a preview mode that only shows the plan, so you can check it before any file changes.

Bulk renaming options in Windows

Method What it does Limits
Highlight several files in File Explorer, press F2 Gives them all one base name plus (1), (2), (3) No dates, no way to keep the original name
PowerToys PowerRename Search and replace in names (free Microsoft tool, separate install) You may not be allowed to install it on a work PC
PowerShell (this post) Any pattern you like, with dates, numbers or prefixes. It also keeps a log of every change You edit a few settings in the script

If you want dates in the names or a way back, PowerShell is the easiest option that needs nothing extra installed.

Before you run it: risks and how to prepare

Renaming does not touch the contents of a file, so nothing gets deleted. Still, a few things can go wrong:

  • You lose track of the original names: without a record, there is no telling which file used to be which
  • Links break: Excel links, shortcuts or other scripts that point at the old name stop working
  • Two files end up with the same name: with a date alone, files from the same day collide

To be safe, copy the folder first and try the script on the copy. The script also protects you in three ways: it only shows the plan while $dryRun is on, it changes nothing at all if any two names would collide, and it writes the log CSV before it touches a single file.

The script

Open PowerShell (5.1 or 7 both work), edit the settings at the top, then paste the whole script. No administrator rights are needed.

# ===== Edit these settings =====
$folder   = "C:\work\Photos-copy"   # the folder with the files (try it on a copy first)
$filter   = "*.jpg"                 # which files ("*" for all)
$prefix   = ""                      # text to put in front, such as "Trip_"
$addDate  = $true                   # add the modified date (20261125)
$addSeq   = $true                   # add a 3-digit number (001)
$keepName = $false                  # keep the original name at the end
$dryRun   = $true                   # while $true, only show the plan
# ===============================

$files = @(Get-ChildItem -LiteralPath $folder -Filter $filter -File |
    Where-Object { $_.Name -notlike "undo-log-*" } | Sort-Object LastWriteTime, Name)
$i = 0
$plan = @(foreach ($f in $files) {
    $i++
    $parts = @()
    if ($addDate)  { $parts += Get-Date $f.LastWriteTime -Format "yyyyMMdd" }
    if ($addSeq)   { $parts += "{0:D3}" -f $i }
    if ($keepName) { $parts += $f.BaseName }
    $new = $prefix + ($parts -join "_") + $f.Extension
    [PSCustomObject]@{
        OldName = $f.Name
        NewName = $new
        OldPath = $f.FullName
        NewPath = Join-Path $f.DirectoryName $new
    }
})
$bad = @($plan | Where-Object {
    ($plan.NewName -eq $_.NewName).Count -gt 1 -or
    ($_.NewName -ne $_.OldName -and (Test-Path -LiteralPath $_.NewPath))
})
$plan | Format-Table OldName, NewName -AutoSize
if ($plan.Count -eq 0) {
    "No matching files were found in the folder."
} elseif ($bad.Count -gt 0) {
    "Stopped. These files would end up with the same name, or the new name is already taken:"
    $bad | Format-Table OldName, NewName -AutoSize
} elseif ($dryRun) {
    "Nothing has changed yet. If the plan looks right, switch dryRun off, then run it again."
} else {
    $log = Join-Path $folder ("undo-log-{0}.csv" -f (Get-Date -Format "yyyyMMdd-HHmmss"))
    $opt = if ($PSVersionTable.PSVersion.Major -ge 6) { @{ Encoding = "utf8BOM" } } else { @{ Encoding = "UTF8"; NoTypeInformation = $true } }
    $plan | Export-Csv -LiteralPath $log @opt
    foreach ($p in $plan) {
        if ($p.NewName -ne $p.OldName) { Rename-Item -LiteralPath $p.OldPath -NewName $p.NewName }
    }
    "Done: {0} files have new names. Undo log: {1}" -f $plan.Count, $log
}

Use and modify the script as you like.

Settings

Line Setting Example
$folder (line 2) The folder with the files (subfolders are not included) "D:\Photos\Trip-copy"
$filter (line 3) Which files are included. "*" means all "*.pdf"
$prefix (line 4) Text at the front. Leave it as "" for none "Invoice_"
$addDate (line 5) Add the modified date as 20261125 $true
$addSeq (line 6) Add a 3-digit number, counted from the oldest modified file $true
$keepName (line 7) Keep the original name at the end $false
$dryRun (line 8) While $true, it only shows the plan. Use $false for the real run $true

The pieces are joined in this order with underscores: prefix, date, number and original name. The extension stays as it was.

What it looks like

I tested it on a folder with IMG_0001.jpg through IMG_0003.jpg, DSC[1].jpg and notes.txt, adding a date and a number (paths shortened). With $dryRun = $true, it shows the plan and leaves the names alone:

OldName      NewName
-------      -------
IMG_0001.jpg 20261101_001.jpg
IMG_0003.jpg 20261103_002.jpg
IMG_0002.jpg 20261105_003.jpg
DSC[1].jpg   20261106_004.jpg

Nothing has changed yet. If the plan looks right, switch dryRun off, then run it again.

The numbers follow the modified time, oldest first. If the plan looks right, change line 8 to $dryRun = $false and paste the script again. After the same table, it prints:

Done: 4 files have new names. Undo log: C:\work\Photos-copy\undo-log-20261125-081959.csv

A log named undo-log-(date and time).csv appears in the folder, with the old and new names and their full paths. The script skips its own log files next time.

"OldName","NewName","OldPath","NewPath"
"IMG_0001.jpg","20261101_001.jpg","C:\work\Photos-copy\IMG_0001.jpg","C:\work\Photos-copy\20261101_001.jpg"
"IMG_0003.jpg","20261103_002.jpg","C:\work\Photos-copy\IMG_0003.jpg","C:\work\Photos-copy\20261103_002.jpg"
"IMG_0002.jpg","20261105_003.jpg","C:\work\Photos-copy\IMG_0002.jpg","C:\work\Photos-copy\20261105_003.jpg"
"DSC[1].jpg","20261106_004.jpg","C:\work\Photos-copy\DSC[1].jpg","C:\work\Photos-copy\20261106_004.jpg"

With the prefix set as Trip_, the number turned off, $keepName turned on, plus every file included, the plan keeps the original names and puts the prefix with the date in front:

OldName      NewName
-------      -------
IMG_0001.jpg Trip_20261101_IMG_0001.jpg
IMG_0003.jpg Trip_20261103_IMG_0003.jpg
IMG_0002.jpg Trip_20261105_IMG_0002.jpg
DSC[1].jpg   Trip_20261106_DSC[1].jpg
notes.txt    Trip_20261107_notes.txt

Undo it with one line

The log lets you put every name back. Replace the path with the log file the script printed, then paste:

Import-Csv -LiteralPath "C:\work\Photos-copy\undo-log-20261125-081959.csv" -Encoding UTF8 | ForEach-Object { Rename-Item -LiteralPath $_.NewPath -NewName $_.OldName }

In my test, the names after the undo matched the originals exactly, in both 5.1 and 7. You can delete the log once you are done.

Things that tripped me up

A date alone gives files from the same day the same name

If you add only the date, two files modified on the same day get the same new name. The script checks for this first and, if it finds even one clash, stops without changing anything:

Stopped. These files would end up with the same name, or the new name is already taken:

OldName      NewName
-------      -------
IMG_0002.jpg 20261103.jpg
IMG_0003.jpg 20261103.jpg

If that can happen in your folder, turn on the number as well ($addSeq = $true). The script also stops when a file with the new name already exists in the folder.

Brackets in names need -LiteralPath

With -Path, the brackets in a name like DSC[1].jpg are read as a wildcard pattern. The script uses -LiteralPath everywhere, so the name is taken as is. DSC[1].jpg was changed and then restored correctly in my test.

The date is the modified date, not the date taken

Both the date and the numbering use the file's last modified time. Copying or editing a photo can change that time, so it may not match the day the photo was taken. Check the dates in the preview before you apply it.

Related posts

To see what is in a folder before you change any names, use Export a folder's file list to CSV for Excel.

Summary

  • Rename-Item handles bulk renames. Choose a date, a number or a prefix in the settings
  • Save the old names in a CSV first, then one line with Import-Csv and Rename-Item undoes everything
  • Try it on a copy and check the preview before you apply it

References

Comments

Copied title and URL