Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 17778

Use PowerShell to Rename Files in Bulk

$
0
0

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 


Viewing all articles
Browse latest Browse all 17778

Trending Articles