PowerShell Script to Rename Multiple Files Easily
Get-ChildItem to list files and Rename-Item to rename them, for example: Get-ChildItem *.txt | Rename-Item -NewName {"new_" + $_.Name} renames all .txt files by adding 'new_' prefix.Examples
How to Think About It
Get-ChildItem. Then, for each file, create a new name by adding or changing parts of the original name. Finally, use Rename-Item to apply the new name to each file.Algorithm
Code
Get-ChildItem -Path . -Filter *.txt | ForEach-Object {
$newName = "new_" + $_.Name
Rename-Item -Path $_.FullName -NewName $newName
Write-Output "Renamed '$($_.Name)' to '$newName'"
}Dry Run
Let's trace renaming 'file1.txt' to 'new_file1.txt' through the code
Get files
Files found: file1.txt, file2.txt, notes.txt
Create new name
For file1.txt, newName = 'new_' + 'file1.txt' = 'new_file1.txt'
Rename file
Rename file1.txt to new_file1.txt
| Original Name | New Name |
|---|---|
| file1.txt | new_file1.txt |
| file2.txt | new_file2.txt |
| notes.txt | new_notes.txt |
Why This Works
Step 1: Get-ChildItem lists files
Get-ChildItem finds all files matching the pattern, like '*.txt', so you only rename the right files.
Step 2: Create new file names
Inside ForEach-Object, you build a new name by adding 'new_' before the original file name using string concatenation.
Step 3: Rename files
Rename-Item changes the file name on disk to the new name you created.
Alternative Approaches
Get-ChildItem -Filter *.txt | ForEach-Object -Begin { $i=1 } -Process {
$newName = "file_" + $i + $_.Extension
Rename-Item $_.FullName -NewName $newName
$i++
Write-Output "Renamed to $newName"
}$map = Import-Csv -Path 'rename_map.csv' foreach ($entry in $map) { Rename-Item -Path $entry.OldName -NewName $entry.NewName Write-Output "Renamed $($entry.OldName) to $($entry.NewName)" }
Complexity: O(n) time, O(1) space
Time Complexity
The script processes each file once, so time grows linearly with the number of files (O(n)).
Space Complexity
The script uses constant extra space, only storing temporary variables for each file (O(1)).
Which Approach is Fastest?
All approaches rename files one by one, so time is similar; using a CSV mapping adds overhead but allows custom names.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Prefix renaming | O(n) | O(1) | Simple bulk renaming with pattern |
| Numbered sequence | O(n) | O(1) | Ordered renaming with numbers |
| CSV mapping | O(n) | O(n) | Custom renaming from external list |
-WhatIf with Rename-Item to preview changes safely.