0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Rename Multiple Files Easily

Use 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

InputFiles: report1.txt, report2.txt; Script: Get-ChildItem *.txt | Rename-Item -NewName {"new_" + $_.Name}
OutputFiles renamed to new_report1.txt, new_report2.txt
InputFiles: image1.jpg, image2.jpg; Script: Get-ChildItem *.jpg | Rename-Item -NewName { $_.BaseName + "_backup" + $_.Extension }
OutputFiles renamed to image1_backup.jpg, image2_backup.jpg
InputFiles: data1.csv, data2.csv; Script: Get-ChildItem *.csv | Rename-Item -NewName { $_.Name.ToUpper() }
OutputFiles renamed to DATA1.CSV, DATA2.CSV
🧠

How to Think About It

To rename multiple files, first get the list of files you want to change using 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

1
Get the list of files matching a pattern or in a folder.
2
For each file, build the new file name based on the original name.
3
Rename the file using the new name.
4
Repeat until all files are renamed.
💻

Code

powershell
Get-ChildItem -Path . -Filter *.txt | ForEach-Object {
    $newName = "new_" + $_.Name
    Rename-Item -Path $_.FullName -NewName $newName
    Write-Output "Renamed '$($_.Name)' to '$newName'"
}
Output
Renamed 'file1.txt' to 'new_file1.txt' Renamed 'file2.txt' to 'new_file2.txt' Renamed 'notes.txt' to 'new_notes.txt'
🔍

Dry Run

Let's trace renaming 'file1.txt' to 'new_file1.txt' through the code

1

Get files

Files found: file1.txt, file2.txt, notes.txt

2

Create new name

For file1.txt, newName = 'new_' + 'file1.txt' = 'new_file1.txt'

3

Rename file

Rename file1.txt to new_file1.txt

Original NameNew Name
file1.txtnew_file1.txt
file2.txtnew_file2.txt
notes.txtnew_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

Using a numbered sequence
powershell
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"
}
Adds a number sequence to file names instead of prefix; useful for ordered renaming.
Using a CSV mapping file
powershell
$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)"
}
Renames files based on a CSV file with old and new names; good for custom renaming lists.

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.

ApproachTimeSpaceBest For
Prefix renamingO(n)O(1)Simple bulk renaming with pattern
Numbered sequenceO(n)O(1)Ordered renaming with numbers
CSV mappingO(n)O(n)Custom renaming from external list
💡
Test your rename script first on a copy of files or use -WhatIf with Rename-Item to preview changes safely.
⚠️
Forgetting to include the file extension in the new name causes files to lose their type and may become unusable.