<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>powershell replace text in string Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/powershell-replace-text-in-string/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/powershell-replace-text-in-string/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Thu, 15 Oct 2020 22:29:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Use Powershell To Replace Text In File</title>
		<link>https://thesysadminchannel.com/powershell-replace-text-in-file/</link>
					<comments>https://thesysadminchannel.com/powershell-replace-text-in-file/#respond</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Fri, 17 Jul 2020 06:05:40 +0000</pubDate>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[powershell replace string in file]]></category>
		<category><![CDATA[powershell replace text in file]]></category>
		<category><![CDATA[powershell replace text in string]]></category>
		<category><![CDATA[Use Powershell To Replace Text In A File]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=2380</guid>

					<description><![CDATA[<p>Time and time again whenever I&#8217;m working with multiple files or working on a script, I need to programmatically use Powershell to replace text in a file. The question I usually ask myself is how can I make Powershell do&#8230; <a href="https://thesysadminchannel.com/powershell-replace-text-in-file/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/powershell-replace-text-in-file/">Use Powershell To Replace Text In File</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Time and time again whenever I&#8217;m working with multiple files or working on a script, I need to programmatically <strong>use Powershell to replace text in a file</strong>. The question I usually ask myself is how can I make Powershell do all the work for me. This is obviously way better than doing it manually so today we&#8217;re going to cross the bridge that&#8217;s ahead of us.</p>
<p>As far as cmdlets are concerned, we&#8217;ll be focusing on <strong>Get-Content</strong> and <strong>Set-Content</strong> to be able to import the text into the shell as well as export the changes.  </p>
<h2>How To Use Powershell To Replace Text In A String</h2>
<p>Let&#8217;s demonstrate this by creating a sample 2-line text file.<br />
&nbsp;</p>
<blockquote style="background-color: #eee"><p>
My favorite animal in the world is a tiger<br />
but I think gorillas are pretty cool too
</p></blockquote>
<p>&nbsp;<br />
The text above is saved to C:\Powershell_Replace.txt and from here we&#8217;re going to load that into memory to be able to play with the output. </p>
<blockquote style="background-color: #eee"><p>
PS C:\> $Content = Get-Content C:\Powershell_replace.txt<br />
PS C:\> $Content.replace(&#8216;tiger&#8217;,&#8217;dog&#8217;)<br />
My favorite animal in the world is a dog<br />
but I think gorillas are pretty cool too
</p></blockquote>
<p>&nbsp;<br />
Let&#8217;s break down what the above code snippet is <strong>actually</strong> doing.</p>
<ul>
<li><strong>$Content</strong> is a variable and we&#8217;re loading the results of <strong>Get-Content</strong> into that variable</li>
<li>Next we use the <strong>Replace Method</strong> to replace text in a string</li>
<li>Since we want to replace &#8220;<em>tiger</em>&#8221; with &#8220;<em>dog</em>&#8220;, we&#8217;ll set &#8220;tiger&#8221; within the parenthesis in quotes, followed by a comma then the text we want to replace it with. In this case it is going to be &#8220;dog&#8221;</li>
<li>Since we&#8217;re not resetting the variable or changing the text in a file, it will output to the console</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-string.png" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-string.png" alt="Replace text in string Powershell" width="1024" height="452" class="aligncenter size-full wp-image-2429" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-string.png?v=1602793099 1024w, https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-string-768x339.png?v=1602793099 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></a><br />
&nbsp;</p>
<h2>How To Use Powershell To Replace Text In File</h2>
<p>So now that we know how to replace text in a string, let&#8217;s take it a bit further and learn how to replace text in a file with Powershell.  We&#8217;ll use the same Powershell_Replace.txt file above as the example file.</p>
<p>Our $Content variable and file still has &#8220;<em>Tiger</em>&#8221; as the favorite animal. It&#8217;s not entirely necessary to reload it into memory again since it&#8217;s already there but generally you&#8217;ll want to make sure you&#8217;re working with the correct content.  Furthermore, now we can use the <strong>Set-Content</strong> to set the content and save it to the file.  Here is what that would look like.</p>
<blockquote style="background-color: #eee"><p>
PS C:\> $Content = Get-Content C:\Powershell_replace.txt<br />
PS C:\> $Content.replace(&#8216;tiger&#8217;,&#8217;dog&#8217;) | Set-Content C:\Powershell_replace.txt<br />
PS C:\> Get-Content C:\Powershell_replace.txt<br />
My favorite animal in the world is a dog<br />
but I think gorillas are pretty cool too
</p></blockquote>
<p>&nbsp;</p>
<p>The output from the replace method is going into the input of the Set-Content cmdlet via the Pipeline so there won&#8217;t be any output on the Powershell console.  However,  if we run Get-Content again, we can see that &#8220;<em>dog</em>&#8221; is now saved in the file.  </p>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file-console.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file-console.png" alt="Powershell Replace text in console" width="1031" height="457" class="aligncenter size-full wp-image-2436" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file-console.png?v=1602795642 1031w, https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file-console-1024x454.png?v=1602795642 1024w, https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file-console-768x340.png?v=1602795642 768w" sizes="(max-width: 1031px) 100vw, 1031px" /></a></p>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file.png" alt="Powershell Replace text in file" width="795" height="278" class="aligncenter size-full wp-image-2433" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file.png?v=1602795404 795w, https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-file-768x269.png?v=1602795404 768w" sizes="(max-width: 795px) 100vw, 795px" /></a><br />
&nbsp;</p>
<h2>How To Use Powershell To Replace Text in Multiple Files</h2>
<p>I&#8217;ll move away from my basic, yet awesome example above into something a little more real world-like.  Let&#8217;s say you have a set of text files that hold certain configs that automagically enable settings on your machine.  Let&#8217;s also say that we just <a href="https://thesysadminchannel.com/demote-or-decommission-a-domain-controller-best-practice/" rel="noopener noreferrer" target="_blank">decommissioned a domain controller</a> and need to update the machine to a valid domain controller.  We want to replace DC01.ad.thesysadminchannel.com to DC03.ad.thesysadminchannel.com as well as replace the IP address to the updated one.</p>
<p>In my example, config1.txt and config2.txt are the essentially the same file and hold the same information.  </p>
<blockquote style="background-color: #eee"><p>
DomainController: DC01.ad.thesysadminchannel.com<br />
IPAddress: 10.0.0.101
</p></blockquote>
<p>&nbsp;</p>
<p>We&#8217;ll capture the file using Get-ChildItem and use a foreach loop to iterate through those files to set the content.  Since we want to change 2 strings, we can append another replace method on top of the existing one to continue to change the content.</p>
<pre class="brush: powershell; title: ; notranslate">
$FileList = Get-ChildItem C:\DCconfigs\ | select FullName

$FileList | ForEach-Object {
    (Get-Content $_.FullName).replace(&quot;DC01.ad.thesysadminchannel.com&quot;,&quot;DC03.ad.thesysadminchannel.com&quot;).Replace(&quot;10.0.0.101&quot;,&quot;10.0.0.103&quot;) | Set-Content $_.FullName
}

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-multiple-files.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-multiple-files.png" alt="Replace text in multiple files Powershell" width="793" height="316" class="aligncenter size-full wp-image-2441" srcset="https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-multiple-files.png?v=1602799870 793w, https://thesysadminchannel.com/wp-content/uploads/2020/10/Powershell-Replace-text-in-multiple-files-768x306.png?v=1602799870 768w" sizes="(max-width: 793px) 100vw, 793px" /></a><br />
&nbsp;</p>
<p>That&#8217;s about it, hopefully you were able to use Powershell to replace text in files and strings.  Also, don&#8217;t forget to stop by and check out our own <a href="https://thesysadminchannel.com/powershell/" rel="noopener noreferrer" target="_blank">Powershell Gallery</a> full of real world useful scripts.  Finally, if you&#8217;re interested, be sure to check out our <a href="https://www.youtube.com/c/theSysadminChannel" rel="noopener noreferrer" target="_blank">Youtube Channel</a> full of awesome system administration content.</p>
<p>The post <a href="https://thesysadminchannel.com/powershell-replace-text-in-file/">Use Powershell To Replace Text In File</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/powershell-replace-text-in-file/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2380</post-id>	</item>
	</channel>
</rss>
