0

Use Powershell To Replace Text In File

Time and time again whenever I’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 all the work for me. This is obviously way better than doing it manually so today we’re going to cross the bridge that’s ahead of us.

As far as cmdlets are concerned, we’ll be focusing on Get-Content and Set-Content to be able to import the text into the shell as well as export the changes.

How To Use Powershell To Replace Text In A String

Let’s demonstrate this by creating a sample 2-line text file.
 

My favorite animal in the world is a tiger
but I think gorillas are pretty cool too

 
The text above is saved to C:\Powershell_Replace.txt and from here we’re going to load that into memory to be able to play with the output.

PS C:\> $Content = Get-Content C:\Powershell_replace.txt
PS C:\> $Content.replace(‘tiger’,’dog’)
My favorite animal in the world is a dog
but I think gorillas are pretty cool too

 
Let’s break down what the above code snippet is actually doing.

  • $Content is a variable and we’re loading the results of Get-Content into that variable
  • Next we use the Replace Method to replace text in a string
  • Since we want to replace “tiger” with “dog“, we’ll set “tiger” 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 “dog”
  • Since we’re not resetting the variable or changing the text in a file, it will output to the console

Replace text in string Powershell
 

How To Use Powershell To Replace Text In File

So now that we know how to replace text in a string, let’s take it a bit further and learn how to replace text in a file with Powershell. We’ll use the same Powershell_Replace.txt file above as the example file.

Our $Content variable and file still has “Tiger” as the favorite animal. It’s not entirely necessary to reload it into memory again since it’s already there but generally you’ll want to make sure you’re working with the correct content. Furthermore, now we can use the Set-Content to set the content and save it to the file. Here is what that would look like.

PS C:\> $Content = Get-Content C:\Powershell_replace.txt
PS C:\> $Content.replace(‘tiger’,’dog’) | Set-Content C:\Powershell_replace.txt
PS C:\> Get-Content C:\Powershell_replace.txt
My favorite animal in the world is a dog
but I think gorillas are pretty cool too

 

The output from the replace method is going into the input of the Set-Content cmdlet via the Pipeline so there won’t be any output on the Powershell console. However, if we run Get-Content again, we can see that “dog” is now saved in the file.

Powershell Replace text in console

Powershell Replace text in file
 

How To Use Powershell To Replace Text in Multiple Files

I’ll move away from my basic, yet awesome example above into something a little more real world-like. Let’s say you have a set of text files that hold certain configs that automagically enable settings on your machine. Let’s also say that we just decommissioned a domain controller 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.

In my example, config1.txt and config2.txt are the essentially the same file and hold the same information.

DomainController: DC01.ad.thesysadminchannel.com
IPAddress: 10.0.0.101

 

We’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.

$FileList = Get-ChildItem C:\DCconfigs\ | select FullName

$FileList | ForEach-Object {
    (Get-Content $_.FullName).replace("DC01.ad.thesysadminchannel.com","DC03.ad.thesysadminchannel.com").Replace("10.0.0.101","10.0.0.103") | Set-Content $_.FullName
}

Replace text in multiple files Powershell
 

That’s about it, hopefully you were able to use Powershell to replace text in files and strings. Also, don’t forget to stop by and check out our own Powershell Gallery full of real world useful scripts. Finally, if you’re interested, be sure to check out our Youtube Channel full of awesome system administration content.

Paul

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.