Windows PowerShell can handle text and strings. The replace function in PowerShell can replace characters, strings, and even text in files. This post from MiniTool Partition Wizard shows you how to use the PowerShell replace function.

## PowerShell Replace String

In this part, I will show you how to replace strings using the PowerShell replace command.

#1. Use the PowerShell Replace () Method

This is the basic usage of PowerShell replace. The syntax is as follows:  \$string.replace(“old”, “new”). For example, if there is a string whose value 'hello, world' and I want to change it to ‘hi, world’, the command should be \$string.replace(“hello”, “hi”).

This PowerShell replace characters in string way is the simplest and you can use it to delete characters in string by specifying an empty string. \$string.replace(“hello”, “”) for an example. In addition, you can replace multiple characters as long as you add a replace () in the command.

#2. Use the PowerShell Replace Operator

The PowerShell replace () method can only replace literal strings. If you want to replace wildcards or regular expressions, you need to use the PowerShell replace operator. The syntax is as follows: \$string -replace “old”, “new”. The PowerShell replace operator can also be used to delete characters and replace multiple characters.

The replace operator can use regex to match the find part. How to use the PowerShell replace regex? The syntax is as follows: “input” -replace “old”, “new”.

## PowerShell Replace Text in File

The PowerShell replace can also be used to replace text in a file. Here is the guide:

• Use the Get-Content command to read a file (txt on the C drive for example). The command should be Get-Content -Path 'C:\file.txt'. Only when the file content is read, you can operate it.
• Use the replace operator to replace text. The command should be (Get-Content C:\file.txt) -replace “old”, “new”.
• Use the Set-Content command to write the text to the file. The command should be Set-Content -Path 'C:\file.txt'.

In general, through the above 3 steps, you can replace text in a file via the PowerShell replace function.

## Bottom Line

Of course, the PowerShell replace function has more advanced usage and most of them combine with regex. If you are interested in PowerShell replace, you need to learn PowerShell replace regex.

In addition, if you have other ideas about PowerShell replace or you know some additional instructions for this function, please share them with us in the following comment zone.

The last but not least, MiniTool Partition Wizard can help you clone the system, manage disks better, and recover data. If you have this need, you can download it from the official website.