Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

Batch rename files using PowerShell and a CSV

Use this simple PowerShell script to change hundreds or thousands of file names automatically, based on a CSV.

As part of a scientific experiment, I had a large number of .JPG images files that I needed to match up to individual plants. Unfortunately the standard “IMG_xxxx.JPG” naming convention had been used. Rather than a meaningless string, I wanted file names to include an identification number as well as some experimental parameters so that I could match the photo to the plant later on.

The following process will allow you change file names en masse based on a CSV file:

Step 1: Create a CSV

Using Microsoft Excel or similar, create a CSV files with two columns:

Create a header for each column:

Populate the CSV as follows:

Path,FileName
c:\path\to\filename1.JPG,newfilename1.JPG
c:\path\to\filename2.JPG,newfilename2.JPG
etc

Save the CSV as “rename_files.csv”. (This process can be automated).

Step 2: Execute the code

The following PowerShell script will manage the process of changing file names:

$folder="c:\path\to\folder" #target folder containing files
$csv="c:\path\to\csv\rename_files.csv" #path to CSV file

cd ($folder); import-csv ($csv)| foreach {rename-item -path $_.path -newname $_.filename}

Further applications

This isn’t limited to JPG files. Any files can be renamed using this script.

This script is particularly useful for instances where large numbers of machine-generated files need to be renamed and organised.

   

Comments

9 responses to “Batch rename files using PowerShell and a CSV”

On 30 May 2018, Oguz wrote: Hyperlink chain icon

Thanks. So handy

Reply

On 13 July 2018, Kal wrote: Hyperlink chain icon

Great script i have this same scenario but I need to do this to a folder and rename with a prefix is that possible??

Example myfile.csv

has entry in there shown below:
folder1
folder2
folder3
etc
Need to change to:
folder1_old
folder2_old
folder3_old
etc

Reply

On 27 March 2019, Benjamin Beebe wrote: Hyperlink chain icon

Here is my CSV:
Path,FileName
C:\Ports\Bills\Test(1).pdf,95-6157877 UCI Emergency Medicine Facult.pdf
C:\Ports\Bills\Test(2).pdf,10-6907025 Iffat Maqbool MD.pdf
C:\Ports\Bills\Test(3).pdf,13-3297333 NY Orthopedics Hospital Associates PC.pdf
C:\Ports\Bills\Test(4).pdf,14-1981653 Meridian Med Group Primary.pdf
C:\Ports\Bills\Test(5).pdf,16-1387862 Quest Diagnostics.pdf
C:\Ports\Bills\Test(6).pdf,20-0463713 Mayfair Emergency Physicians.pdf
C:\Ports\Bills\Test(7).pdf,20-0495416 Hilton Head Emergency Physicians.pdf
C:\Ports\Bills\Test(8).pdf,20-1218565 Northstar Anesthesia PA.pdf
C:\Ports\Bills\Test(9).pdf,20-1976901 Revival HHC.pdf
C:\Ports\Bills\Test(10).pdf,20-3819486 Advamced Medical Group.pdf
C:\Ports\Bills\Test(11).pdf,20-4962339 David H Kim MD Inc.pdf
C:\Ports\Bills\Test(12).pdf,20-5306119 Alexander J Shen MD Inc.pdf
C:\Ports\Bills\Test(13).pdf,20-8139290 Watkins Spine Inc.pdf
C:\Ports\Bills\Test(14).pdf,20-8545497 PV Peninsula Plastic Surgery Ctr.pdf
C:\Ports\Bills\Test(15).pdf,20-8584688 Anesthesia Care Consultants Inc.pdf
C:\Ports\Bills\Test(16).pdf,20-8978118 Anesthesia Consultants of Savannah.pdf

Here is my PowerShell script:
$folder=”C:\Ports\Bills”
$csv=”C:\Ports\rename_files.csv”

cd ($Bills); import-csv ($csv)| foreach {rename-item -path $_.path -newname $_.filename}

I cannot get this to work. It says file does not exist.

Reply

    On 19 June 2019, Riker wrote: Hyperlink chain icon

    Enclose your new names in quotes since there are spaces in file names

    Reply

On 20 August 2019, Jay wrote: Hyperlink chain icon

I have a csv file that has three columns.

ID,Name,Size
00P3400000cd7rsEAA,201516003_8D_FINAL.doc,4236288

I can rename the files using the first two columns but need to be able to use the size column to make sure the script is renaming the correct file. There are duplicate Name names.

Here is what I have so far:

Import-Csv test.csv | ForEach-Object{
Rename-Item $_.ID ????? and $_Size?????? $_.Name
}
}

Reply

On 4 January 2020, Alvina wrote: Hyperlink chain icon

I cant get it to work. the following errors appear:
import-csv : Access to the path ‘C:\Users\Alvina\Desktop\Staff’ is denied.
At line:4 char:15
+ cd ($folder); import-csv ($csv)| foreach {rename-item -path $_.path – …
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], UnauthorizedAccessException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand

Reply

    On 16 February 2021, p wrote: Hyperlink chain icon

    If you’re editing your CSV file in Excel, try File > Save As > More Options > Save as type: > CSV (MS-DOS) (*.csv)

    Reply

On 14 April 2022, Ian wrote: Hyperlink chain icon

Can you use alter the script to make a copy of the file, then rename it and place it in a new folder? Basically, I want to keep the original file and make a copy and rename it. Is that possible?

Reply

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.