{"id":2525,"date":"2013-11-22T00:01:00","date_gmt":"2013-11-22T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/11\/22\/use-powershell-to-rename-files-in-bulk\/"},"modified":"2013-11-22T00:01:00","modified_gmt":"2013-11-22T00:01:00","slug":"use-powershell-to-rename-files-in-bulk","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-rename-files-in-bulk\/","title":{"rendered":"Use PowerShell to Rename Files in Bulk"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to use Windows PowerShell to rename files in bulk.\nMicrosoft Scripting Guy, Ed Wilson, is here. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/matt+tisdale\/\" target=\"_blank\">Matt Tisdale<\/a> is back today with another solution to a situation at work&hellip;\nI 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&mdash;and that was before I even heard the question.\nHis 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&#8217;s say he needed to find all files with &#8220;current&#8221; in the name and replace &#8220;current&#8221; with &#8220;old&#8221;.\nI have never attempted this specific task, but by using <strong>Get-Command <\/strong>and <strong>Get-Help<\/strong>,<strong> <\/strong>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 &#8220;current&#8221; in the name and rename them using &#8220;old&#8221; in place of &#8220;current&#8221;, here are the steps.<\/p>\n<ol>\n<li>Open Windows PowerShell.<\/li>\n<li>Navigate to C:temp.<\/li>\n<li>Run the following Windows PowerShell command:<\/li>\n<\/ol>\n<p style=\"padding-left: 30px\">Get-ChildItem -Filter &#8220;*current*&#8221; -Recurse | Rename-Item -NewName {$_.name -replace &#8216;current&#8217;,&#8217;old&#8217; }\n&nbsp; &nbsp; &nbsp; 4.&nbsp;Sit back and let Windows PowerShell do all of the work for you.\nAnyone who has access to modify the files and is running Windows&nbsp;7 can follow these steps. No special tools or elevated rights are required.\nLet&#8217;s pick the command apart and explain each piece.<\/p>\n<ul>\n<li><strong>Get-ChildItem -Filter &#8220;*current*&#8221; <\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px\">Finds all files with the string &#8220;current&#8221; anywhere in the name.<\/p>\n<ul>\n<li><strong>-Recurse <\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px\">Instructs <strong>Get-ChildItem<\/strong> to search recursively through all subfolders under the start point (the current directory where the command is run from).<\/p>\n<ul>\n<li><strong>|<\/strong> (the pipe character)<\/li>\n<\/ul>\n<p style=\"padding-left: 30px\">Instructs Windows PowerShell to take each item (object) found with the first command (<strong>Get-ChildItem<\/strong>) and pass it to the second command (<strong>Rename-Item<\/strong>)<\/p>\n<ul>\n<li><strong>Rename-Item <\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px\">Renames files and other objects<\/p>\n<ul>\n<li><strong>{$_.name -replace &#8216;current&#8217;,&#8217;old&#8217; } <\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px\">Tells <strong>Rename-Item<\/strong> to find the string &#8220;current&#8221; in the file name and replace it with &#8220;old&#8221;.\nFor an added bonus, I also recommended that Cristofer use the &#8211;<strong>WhatIf<\/strong> 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 <strong>-WhatIf<\/strong> will tell Windows PowerShell to run the <strong>Rename-Item<\/strong> portion of this command in <strong>Report Only<\/strong> mode. It will not actually rename anything&mdash;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 <strong>-WhatIf<\/strong> parameter added.<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Filter &#8220;*current*&#8221; -Recurse | Rename-Item -NewName {$_.name -replace &#8216;current&#8217;,&#8217;old&#8217; } -whatif\nUnfortunately, when we use <strong>&ndash;WhatIf<\/strong>, we cannot send our output to a text log file. If you need a log file that shows the results of using <strong>-WhatIf<\/strong>, you can follow these steps:<\/p>\n<ol>\n<li>Open Windows PowerShell.<\/li>\n<li>Run <strong>Start-Transcript<\/strong>.<\/li>\n<li>Run your command (including the <strong>-WhatIf<\/strong> parameter).<\/li>\n<li>After the command completes, run <strong>Stop-Transcript<\/strong>.<\/li>\n<li>Note the location and file name of the transcript file, and open this file to see the results.<\/li>\n<\/ol>\n<p>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 &#8220;fun&#8221; 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.\nKeep the requests coming in. I always enjoy helping fellow employees find ways to be more efficient and save time &nbsp;and money.\n~Matt\nThanks again, Matt, for sharing your time and knowledge. This is awesome.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip; I received a call from a gentleman named Cristofer this afternoon. He had a business need and he heard from someone that Windows [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,56,433,3,12,45],"class_list":["post-2525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-guest-blogger","tag-matt-tisdale","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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&hellip; I received a call from a gentleman named Cristofer this afternoon. He had a business need and he heard from someone that Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2525"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2525\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}