Skip to content

Batch rename files using PowerShell and a CSV

G23rd November 2015

C7 Comments

TPowerShell

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:

  • Column 1 = Current file name, including path
  • Column 2 = New file name

Create a header for each column:

  • Column 1 = Path
  • Column 2 = FileName

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:

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

  • Written by Oguz on 30 May 2018:

    Thanks. So handy

  • Written by Kal on 13 July 2018:

    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

  • Written by Benjamin Beebe on 27 March 2019:

    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.

    • Written by Riker on 19 June 2019:

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

  • Written by Jay on 20 August 2019:

    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
    }
    }

  • Written by Alvina on 4 January 2020:

    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

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.