Replacing Strings in a Text File using PowerShell

Do you have a text file where you need to replace a specific word, part of a word, or selected characters? Typically, you would open the file in a text editor and perform the search and replace operation for every word you wish to replace — which is tedious, even for one file.

What more if you need to do the same actions over multiple and large text files? Humans are not designed to perform repetitive tasks 100% without fail. In this case, you will need to automate, and one of the best ways to do that is with the PowerShell replace string in file technique.

And now that there’s a PowerShell version for Windows, Linux, and macOS, there’s no more reason for you not to try the replace string PowerShell method.

The examples in this post are performed in PowerShell 7.2.5 on a Windows 10 computer.

Reading the Text File

Let’s start by creating a sample text file you’ll use throughout this article. This way, the examples will be consistent with the sample strings should you follow along.

Open a PowerShell window and run the below code. This code creates a new file called sample.txt on the current directory.

Today, I ate pancakes for breakfast. 
Now I'm looking forward to having a turkey sandwich for lunch. 
I haven't decided between steak and pizza for dinner yet. What do you think? 
'@ | Out-File .\sample.txt

Now that you have the sample.txt file import the file’s contents using the Get-Content cmdlet and store it into a variable called $text. The -Raw switch imports the text file as a single string value and not a collection of strings.

$text = Get-Content .\sample.txt -Raw

replace string powershell

Replace String PowerShell Operator and Method

PowerShell offers two ways of replacing strings. These are the [-replace] operator and [replace()] method. Both methods produce the same result as far as replacing strings goes.

Which method you choose depends on familiarity, purpose, or personal preference.

Remember that, at this point, you have the contents of the text file stored in the $text variable. This means that the PowerShell replace string in file operation will be done to text stored in the variable and not directly to the file itself.

For example, run any of the commands below to replace the word pancakes with hash browns.

# Using the Replace operator 
$text -replace 'pancakes','hash browns' 

# Using the Replace operator 
$text.Replace('pancakes','hash browns')

As you can see below, both commands produced the same results—the word pancakes was replaced with hash browns.

powershell replace string in file
Importing a text file

Uppercase and Lowercase String Replacement

By default, the -replace operator is case-insensitive, which means it will replace the matching strings while ignoring character cases. For example, the words Today and today are valid matches for the -replace operator.

So if you need to perform a case-sensitive replace string PowerShell method, you must use the -creplace operator instead.

$text -creplace 'Today','Yesterday'

powershell replace string in a file

In contrast, the replace() method is case-sensitive by default. For example, the replace() command below will not replace the words turkey because the old string to replace starts with an uppercase T.

$text.Replace('Turkey', 'ham')

replace string in a file powershell

To make the replace() method ignore the character case, you must add two more overloads to the command. The syntax is as follows.

  • ignoreCase accepts a Boolean value. $True means case-insensitive, and $False means case-sensitive.
  • cultureInfo is the culture information object as the basis for the character types.

Note. To get a list of all culture information for reference, run Get-Culture -ListAvailable.

With those in mind, the correct command to perform case-insensitive are below.

# Example 1: Using the system's current culture information 
$text.Replace('Turkey', 'ham', $true, $(Get-Culture)) 

# Example 2: Using a specific culture (ie. en-US, fr-BE, etc.) 
$text.Replace('Turkey', 'ham', $true, $(Get-Culture 'en-US'))

The result below shows that the command replaced turkey with ham since the replace method ignored the character case.

replace string in a file using powershell

Replacing Multiple Instances

If you need to replace strings in one go, you can string together multiple replace statements.

$text -replace 'pancakes','hash browns' -replace 'Turkey', 'ham' -replace 'today', 'Yesterday' 

$text.Replace('pancakes','hash browns').Replace('turkey', 'ham').Replace('Today', 'Yesterday')

You can see below that you do not have to replace multiple strings in separate commands. Stitching together the replace operators or methods, you can replace as many strings as you need in a single line.

replace powershell

Writing Back the Results

Once you’ve finished replacing the strings, you can finally write the results back to the same or new text file. You have two options to write the changes; using the Out-File or Set-Content cmdlets. You must pipe the result to either of these commands.

Important. Should you use Out-File or Set-Content? In this specific case, either is fine. The main difference between these two is file locking.

Other processes still read the file even if the Out-File is running. The Set-Content cmdlet locks the file, and no other processes can read it while the content is being written.*

# Pipe to Out-File 
$text -replace 'pancakes','hash browns' -replace 'Turkey', 'ham' -replace 'today', 'Yesterday' | Out-File .\sample.txt 
# Pipe to Set-Content 
$text -replace 'pancakes','hash browns' -replace 'Turkey', 'ham' -replace 'today', 'Yesterday' | Set-Content .\sample.txt

replace powershell text

Leave a Reply

Your email address will not be published. Required fields are marked *