This will be useful if you are working with strings in PowerShell automation. Here, I have explained, how to replace text in strings using PowerShell using different methods.
To replace text in strings using PowerShell, you can use the Replace() method for simple, case-sensitive replacements or the -replace operator for more complex, case-insensitive, and regex-based replacements. For example, $string.Replace("old", "new") replaces all occurrences of “old” with “new”, while $string -replace "(?i)old", "new" performs a case-insensitive replacement.
Replace Text in Strings Using PowerShell
Now, let me show you a few useful methods to replace text in strings in PowerShell with examples.
Using the Replace() Method
The Replace() method is a very useful method to replace text in a string in PowerShell. This method is case-sensitive and replaces all occurrences of a specified substring with a new substring.
Let me show you an example.
Example:
$string = "Hello World"
$newString = $string.Replace("World", "PowerShell")
Write-Output $newString # Output: Hello PowerShellIn this example, the word “World” is replaced with “PowerShell”.
I executed the above script, and you can see the output in the screenshot below:

Using the -replace Operator
In PowerShell, you can also use -replace Operator to replace text in strings using PowerShell.
The -replace operator in PowerShell is more flexible than the Replace() method because it supports regular expressions. Let me show you an example.
Example:
$string = "Hello World"
$newString = $string -replace "World", "PowerShell"
Write-Output $newString # Output: Hello PowerShellJust like the Replace() method, this replaces “World” with “PowerShell”.
You can see the output in the screenshot below after I executed the above PowerShell script.

Check out Count Occurrences of a Substring in a String in PowerShell
Case-Insensitive Replacement using PowerShell
To perform a case-insensitive replacement, you can use the -replace operator with the (?i) regex option in PowerShell.
Let me show you an example.
Example:
$string = "Hello WORLD"
$newString = $string -replace "(?i)world", "PowerShell"
Write-Output $newString # Output: Hello PowerShellHere, the (?i) option makes the replacement case-insensitive, so “WORLD” is replaced with “PowerShell”.
Replace Multiple Characters using PowerShell
You can replace multiple characters by chaining multiple Replace() method calls or using a more complex regular expression with the -replace operator.
Let me show you a few examples.
Example with Replace() Method:
$string = "Hello-World-!"
$newString = $string.Replace("-", " ").Replace("!", "")
Write-Output $newString # Output: Hello World Example with -replace Operator:
$string = "Hello-World-!"
$newString = $string -replace "[-!]", " "
Write-Output $newString # Output: Hello World In these examples, both - and ! are replaced with spaces.
Read Remove Characters from Strings in PowerShell
Conditional Replacement in PowerShell
You can also perform conditional replacements using PowerShell. For instance, you might want to replace text only if it meets certain criteria. Let me show you an example.
Example:
$string = "The quick brown fox jumps over the lazy dog"
if ($string -match "fox") {
$newString = $string -replace "fox", "cat"
}
Write-Output $newString # Output: The quick brown cat jumps over the lazy dogIn this example, “fox” is replaced with “cat” only if “fox” is found in the string.
You can see the output in the screenshot below:

Replace Text in Multiple Lines in PowerShell
When working with multi-line strings, you can use the -replace operator to replace text across all lines in PowerShell. Let me show you an example.
Example:
$string = @"
Hello World
Hello PowerShell
"@
$newString = $string -replace "Hello", "Hi"
Write-Output $newStringOutput:
Hi World
Hi PowerShellIn this example, “Hello” is replaced with “Hi” on both lines.
Conclusion
In this tutorial, I have explained how to replace text in strings in PowerShell using the Replace() Method and Using the -replace Operator with various examples.
You may also like the following tutorials:
- Extract Strings Between Parentheses in PowerShell
- Remove Characters from Strings in PowerShell
- Extract Text from Strings Between Delimiters Using PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.