Replace a Text in File Using PowerShell

Rohan Timalsina Feb 06, 2022
  1. Use Get-Content and Set-Content to Replace Every Occurrence of a String in a File With PowerShell
  2. Use File Class to Replace Every Occurrence of a String in a File With PowerShell
  3. Replace Every Occurrence of a String in Multiple Files With PowerShell
Replace a Text in File Using PowerShell

PowerShell is a powerful tool that can perform different files and folders operations. It allows you to create, copy, delete, move, rename, and view files and folders on the system.

PowerShell has some useful cmdlets that can read, write, and replace content in a file. This tutorial will introduce different methods to replace every string occurrence in a file with PowerShell.

Use Get-Content and Set-Content to Replace Every Occurrence of a String in a File With PowerShell

The Get-Content gets the item’s content in the specified path, such as the text in a file. The Set-Content is a string-processing cmdlet that allows you to write new content or replace the existing content in a file.

You can use the Get-Content and Set-Content cmdlet to replace every string occurrence in a file. We have a text file (test.txt) in the directory (C:\New) with the following content.

Get-Content C:\New\test.txt

Output:

Welcome to Linux tutorials.
Linux is free.
Linux is powerful.

Now, let’s replace every occurrence of a string Linux with PowerShell in a test.txt file with the help of the -Replace parameter. The parentheses () are required around the Get-Content.

(Get-Content C:\New\test.txt) -Replace 'Linux', 'PowerShell' | Set-Content C:\New\test.txt

Then view the content of a test.txt file to verify the changes.

Get-Content C:\New\test.txt

As you can see, Linux has been successfully replaced with PowerShell.

Output:

Welcome to PowerShell tutorials.
PowerShell is free.
PowerShell is powerful.

This method uses a string array to find and replace strings in a file because the Get-Content cmdlet returns an array. It is easier to replace strings if the Get-Content returns a single string.

You can use the -Raw parameter as shown below.

(Get-Content C:\New\test.txt -Raw) -Replace 'Linux', 'PowerShell' | Set-Content C:\New\test.txt

Use File Class to Replace Every Occurrence of a String in a File With PowerShell

The File class provides static methods for common operations such as creating, copying, moving, opening, deleting, and appending to a single file. Use the File class’ Replace() method to replace the contents of a specified file.

Get-Content C:\New\python.txt
Find the best Python tutorials and learn Python easily from DelftStack.

Here is an example to replace every occurrence of a string in a file using the File class methods.

$string = [System.IO.File]::ReadAllText("C:\New\python.txt").Replace("Python","JavaScript")
[System.IO.File]::WriteAllText("C:\New\python.txt", $string)

The ReadAllText() method opens a text file, reads all the text in that file, and then closes the file.

The WriteAllText() method creates a new file, writes the specific string to the file, and closes the file. It is overwritten if the target file already exists in the location.

Verify the changes made in C:\New\python.txt.

Get-Content C:\New\python.txt

Output:

Find the best JavaScript tutorials and learn JavaScript easily from DelftStack.

Replace Every Occurrence of a String in Multiple Files With PowerShell

The above methods replace a string in a single file; sometimes, you might need to replace the same string in multiple files. In that case, you can use the command below to replace every occurrence of a specified string in multiple files.

Get-ChildItem 'C:\New\*.txt' | ForEach {
     (Get-Content $_) | ForEach  {$_ -Replace 'weekly', 'monthly'} | Set-Content $_
}

The Get-ChildItem cmdlet gets the files in the specified directory C:\New. The asterisk * wildcard specifies all files with the filename extension .txt.

Within a ForEach loop, you can run one or more commands against each item in an array.

You can use the -Recurse parameter to replace a string in files in the specified directory and its subdirectories.

Get-ChildItem 'C:\New\*.txt' -Recurse | ForEach {
     (Get-Content $_) | ForEach  {$_ -Replace 'weekly', 'monthly'} | Set-Content $_
}
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell String