1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
Welcome to GadgetBit!

We are a community of technology enthusiats.

If you are already a registered member then you can login here

If you are new to the site then why not join up? It will only take a couple of minutes to register, by doing so all adverts will be removed from the site. You can register here or you can use your Facebook or Twitter account.

Batch File Rename with Windows PowerShell

  1. Creaky
    Microsoft included a batch rename feature in the latest version of Windows allowing you to select multiple files, right click on one and select rename. All of the selected files will be renamed with the name you provided and a number. This functionality works well for basic files but does not provide any flexibility in exactly how the files are numbered and also does not allow the file extension to be changed.

    Windows PowerShell, which is included in Windows starting with Vista and 2008 Server and available for XP, provides the raw tools to allow you to perform batch file renames. The trick is to pipe the output of one command into another command allowing you to connect the functionality to produce a useful outcome.
    The two PowerShell commands needed for batch file renaming are dir (which is an alias for get-childitem) and rename-item.

    To get started I suggest copying all the files you need to rename to a separate directory. Then, open up Windows PowerShell and navigate to the directory with the CD command.


    Depending on how you want to rename the files there are a few different techniques:

    Changing the File Extension of all .jpeg files to .jpg

    Dir *.jpeg | rename-item -newname { $_.name -replace ".jpeg",".jpg" }
    In the above example the $_ represents each item passed to the rename-item command via the pipe | from the dir command.

    Appending a File Extension

    Dir | rename-item -newname { $_.Name +".jpg" }

    File Rename with Customizable Increasing Number

    Dir *.jpg | ForEach-Object -begin { $count=1 } -process { rename-item $_ -NewName "image$count.jpg"; $count++ }

    In the example we needed to use an additional command known as foreach-object that allows you to set a variable (our counter) before looping through each file in the directory and then do an action for each item. The result of the above example are files named image1.jpg, image2.jpg and so on.

    Share This Article

Comments

To make a comment simply sign up and become a member!