Use PowerShell to Rename Files in Bulk

Use PowerShell to Rename Files in Bulk

  • Comments 21
  • Likes

Summary: Learn how to use Windows PowerShell to rename files in bulk.

Microsoft Scripting Guy, Ed Wilson, is here. Matt Tisdale is back today with another solution to a situation at work…

I received a call from a gentleman named Cristofer this afternoon. He had a business need and he heard from someone that Windows PowerShell could help. I told him that I am sure Windows PowerShell can help—and that was before I even heard the question.

His immediate need was to rename almost 250 files that are located in various folders on the file system. He needed to find all files with a specific character string in the name and replace this character string with a new character string. For example purposes, let's say he needed to find all files with "current" in the name and replace "current" with "old".

I have never attempted this specific task, but by using Get-Command and Get-Help, we were able to find exactly how to do this in a couple of minutes. Assuming we need to find all files located under C:\temp (including all subfolders) with "current" in the name and rename them using "old" in place of "current", here are the steps.

  1. Open Windows PowerShell.
  2. Navigate to C:\temp.
  3. Run the following Windows PowerShell command:

Get-ChildItem -Filter "*current*" -Recurse | Rename-Item -NewName {$_.name -replace 'current','old' }

      4. Sit back and let Windows PowerShell do all of the work for you.

Anyone who has access to modify the files and is running Windows 7 can follow these steps. No special tools or elevated rights are required.

Let's pick the command apart and explain each piece.

  • Get-ChildItem -Filter "*current*"

Finds all files with the string "current" anywhere in the name.

  • -Recurse

Instructs Get-ChildItem to search recursively through all subfolders under the start point (the current directory where the command is run from).

  • | (the pipe character)

Instructs Windows PowerShell to take each item (object) found with the first command (Get-ChildItem) and pass it to the second command (Rename-Item)

  • Rename-Item

Renames files and other objects

  • {$_.name -replace 'current','old' }

Tells Rename-Item to find the string "current" in the file name and replace it with "old".

For an added bonus, I also recommended that Cristofer use the -WhatIf parameter with this command on his first run to get an output that shows what the command will do before he actually pulls the trigger. Using -WhatIf will tell Windows PowerShell to run the Rename-Item portion of this command in Report Only mode. It will not actually rename anything—it will simply show what files were found that match the filter criteria and what each new name would be. Here is the command with the -WhatIf parameter added.

Get-ChildItem -Filter "*current*" -Recurse | Rename-Item -NewName {$_.name -replace 'current','old' } -whatif

Unfortunately, when we use –WhatIf, we cannot send our output to a text log file. If you need a log file that shows the results of using -WhatIf, you can follow these steps:

  1. Open Windows PowerShell.
  2. Run Start-Transcript.
  3. Run your command (including the -WhatIf parameter).
  4. After the command completes, run Stop-Transcript.
  5. Note the location and file name of the transcript file, and open this file to see the results.

It is always fun to solve business challenges with Windows PowerShell commands. Cristofer said he will probably need to use these commands for renaming 1000+ files in the near future, and he will share this process with other members of his team. Imagine how much "fun" it would be to rename all of these files manually or with some clunky old batch file. No thank you. I will stick with Windows PowerShell.

Keep the requests coming in. I always enjoy helping fellow employees find ways to be more efficient and save time  and money.

~Matt

Thanks again, Matt, for sharing your time and knowledge. This is awesome.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks!  You are a very talented writer.  This was very thorough and easy to follow.

  • Very useful tip. My problem would be a little bit different. I am sure that it still could be done. My issue would be that there are a group of files with a common string. Is there a way to rename the file based on the created date of the file. I would like the output to be something like CommonString_ddMMMYY

  • I used similar method but no success when "[" is part of file name. Any ideas?

  • I was able to figure it out . Thanks to this tip  Thank you

    This is what I came up with

    Get-ChildItem -Filter "*.csv" -Recurse | Rename-Item -NewName {$_.BaseName + $_.CreationTime.toString("ddMMMyy") + ".csv" }

  • @n

    "[" is a special character in certain PowerShell commands that accept wildcards.  To use any of those special characters as literals, you have to place them inside a pair of square brackets (though this doesn't seem to apply to the -Filter parameter of Get-ChildItem, it does apply to -Path.)  For example, both of these work on my system:

    Get-ChildItem -Path '*[[]*'

    Get-ChildItem -Filter '*[*'

  • n,

    My assumption is that what is not working for you is renaming a file containing "[" where you are trying to replace "[" with some other character.  If this is the case, "[" is a special character and must be "escaped" with a backslash character.  Here is an example… I want to find all files in a folder containing "[" and replace each "[" character with a "-" character.  Here is the command:

    Get-ChildItem -Filter "*[*" | Rename-Item -NewName {$_.name -replace "\[", "-"}

    If this does not solve your issue post another questions and I will provide feedback as soon as possible.

    character and must be "escaped"

  • Thanks.

  • The problem is that PowerShell cannot see files with "[" in filename. In spite of that I am not able to do any rename operation - I get an error "file does not exist".

  • PowerShell does see files with the bracket character.  I created a number of test files earlier with this character at various places in the file name and ran the command I posted earlier today.  It found and renamed the files just fine.  I am running PS 3.0.  Maybe you are running an older version and it has an issue.  I am not sure if older version have this kind of problem or not.  Feel free to post the exact command you are running and I will do some testing with it.

  • n,

    Try the -literalpath parameter... it's function is descirbed in the help

    get-help Get-ChildItem -Full

  • Thanks a lot!!! It worked! I have visited many websites and have tried many different Things and thgis one worked! Very well explained!

  • Good Post and helped

  • Awesome!

  • This just came in handy, big time. Thank you.

  • Thanks for the post, help me a lot, but i need one more help:
    What can i do if i don´t know how character i´m seeking, because the explorer don´t show it and in the PS prompt the character only appear a question mark (i don´t know if really is)? I need to remove the character.

    Thnak you