{"id":40,"date":"2013-08-13T21:10:50","date_gmt":"2013-08-13T21:10:50","guid":{"rendered":"http:\/\/www.edrockwell.com\/blog\/?p=40"},"modified":"2013-08-14T16:03:07","modified_gmt":"2013-08-14T16:03:07","slug":"how-to-search-files-recursivly-and-replace-content-in-files","status":"publish","type":"post","link":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/","title":{"rendered":"How to Search Files Recursivly and Replace content in files"},"content":{"rendered":"<p>I was tasked to search many files in a directory structure and find content in each file and replace that content. The directory has MANY types of files in it so I had to search for a certain type of file, then search inside that file and replace a string if found.<\/p>\n<p>So, I did this:<\/p>\n<p>Get-ChildItem | ForEach-Object Get-Content -replace and then Set-Content<\/p>\n<p>change *File.Type* with the filetype you want to search for. It could be a *.log or a *.bak<\/p>\n<p>Change string1 to what you&#8217;re looking for and string2 to what you want to change it to.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>NOTE:<\/strong><\/span> This code will save the file with a new date even if it doesn&#8217;t replace the string. One minor issue that you must be aware of because if you later want to search for all old files and delete them, it wont find old ones that you&#8217;ve touched with this script because of the date change. I&#8217;m working on a new version that only saves the file if the string is found in it. Feel free to comment if you got the answer to this \ud83d\ude42<\/p>\n<p>Here is the code:<\/p>\n<pre lang=\"POWERSHELL\">Get-ChildItem -include *File.Type* -Recurse | ForEach-Object { ( Get-Content -Path $_.FullName ) -replace 'string1', 'string2' | set-content $_.fullname }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was tasked to search many files in a directory structure and find content in each file and replace that content. The directory has MANY types of files in it so I had to search for a certain type of file, then search inside that file and replace a string if found. So, I did [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[30,28,25,24,26,31,29,27,32],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-files","tag-find","tag-foreach-object","tag-get-childitem","tag-get-content","tag-recursivly","tag-replace","tag-set-content","tag-string"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Search Files Recursivly and Replace content in files - System Admin [RESOLVED]<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Search Files Recursivly and Replace content in files - System Admin [RESOLVED]\" \/>\n<meta property=\"og:description\" content=\"I was tasked to search many files in a directory structure and find content in each file and replace that content. The directory has MANY types of files in it so I had to search for a certain type of file, then search inside that file and replace a string if found. So, I did [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\" \/>\n<meta property=\"og:site_name\" content=\"System Admin [RESOLVED]\" \/>\n<meta property=\"article:published_time\" content=\"2013-08-13T21:10:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-08-14T16:03:07+00:00\" \/>\n<meta name=\"author\" content=\"Ed Rockwell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EddieRock\" \/>\n<meta name=\"twitter:site\" content=\"@EddieRock\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ed Rockwell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\"},\"author\":{\"name\":\"Ed Rockwell\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\"},\"headline\":\"How to Search Files Recursivly and Replace content in files\",\"datePublished\":\"2013-08-13T21:10:50+00:00\",\"dateModified\":\"2013-08-14T16:03:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\"},\"wordCount\":203,\"commentCount\":0,\"keywords\":[\"files\",\"find\",\"foreach-object\",\"get-childitem\",\"get-content\",\"recursivly\",\"replace\",\"set-content\",\"string\"],\"articleSection\":[\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\",\"url\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\",\"name\":\"How to Search Files Recursivly and Replace content in files - System Admin [RESOLVED]\",\"isPartOf\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#website\"},\"datePublished\":\"2013-08-13T21:10:50+00:00\",\"dateModified\":\"2013-08-14T16:03:07+00:00\",\"author\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.edrockwell.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Search Files Recursivly and Replace content in files\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#website\",\"url\":\"https:\/\/www.edrockwell.com\/blog\/\",\"name\":\"System Admin [RESOLVED]\",\"description\":\"How To: Make My DevOps Life Easier\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.edrockwell.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\",\"name\":\"Ed Rockwell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"caption\":\"Ed Rockwell\"},\"sameAs\":[\"http:\/\/www.therockwells.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Search Files Recursivly and Replace content in files - System Admin [RESOLVED]","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/","og_locale":"en_US","og_type":"article","og_title":"How to Search Files Recursivly and Replace content in files - System Admin [RESOLVED]","og_description":"I was tasked to search many files in a directory structure and find content in each file and replace that content. The directory has MANY types of files in it so I had to search for a certain type of file, then search inside that file and replace a string if found. So, I did [&hellip;]","og_url":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/","og_site_name":"System Admin [RESOLVED]","article_published_time":"2013-08-13T21:10:50+00:00","article_modified_time":"2013-08-14T16:03:07+00:00","author":"Ed Rockwell","twitter_card":"summary_large_image","twitter_creator":"@EddieRock","twitter_site":"@EddieRock","twitter_misc":{"Written by":"Ed Rockwell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#article","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/"},"author":{"name":"Ed Rockwell","@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7"},"headline":"How to Search Files Recursivly and Replace content in files","datePublished":"2013-08-13T21:10:50+00:00","dateModified":"2013-08-14T16:03:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/"},"wordCount":203,"commentCount":0,"keywords":["files","find","foreach-object","get-childitem","get-content","recursivly","replace","set-content","string"],"articleSection":["PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/","url":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/","name":"How to Search Files Recursivly and Replace content in files - System Admin [RESOLVED]","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/#website"},"datePublished":"2013-08-13T21:10:50+00:00","dateModified":"2013-08-14T16:03:07+00:00","author":{"@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7"},"breadcrumb":{"@id":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.edrockwell.com\/blog\/how-to-search-files-recursivly-and-replace-content-in-files\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edrockwell.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Search Files Recursivly and Replace content in files"}]},{"@type":"WebSite","@id":"https:\/\/www.edrockwell.com\/blog\/#website","url":"https:\/\/www.edrockwell.com\/blog\/","name":"System Admin [RESOLVED]","description":"How To: Make My DevOps Life Easier","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edrockwell.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7","name":"Ed Rockwell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","caption":"Ed Rockwell"},"sameAs":["http:\/\/www.therockwells.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":5,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}