How to Replace Text in Strings Using PowerShell?

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 PowerShell

In this example, the word “World” is replaced with “PowerShell”.

I executed the above script, and you can see the output in the screenshot below:

Replace Text in Strings Using PowerShell

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 PowerShell

Just 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.

How to Replace Text in Strings Using PowerShell

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 PowerShell

Here, 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 dog

In this example, “fox” is replaced with “cat” only if “fox” is found in the string.

You can see the output in the screenshot below:

Conditional Replacement in PowerShell

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 $newString

Output:

Hi World
Hi PowerShell

In 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.