How to Replace Part of a String Using Powershell
There are two very simple ways to replace text within a string in Powershell.
First, let’s look at using the replace function.
Using the replace() function
You can replace part of a string using the replace function:
$str = "Hello world!"
$str.replace("world", "Mars")
Hello Mars!
There are two things to note here:
- Parameters are case sensitive
- The function will replace every instance of the string it finds
For instance, if we attempt the same with a more repetitive string:
$str = "Hello, hello, hello world!"
$str.replace("hello", "ello")
Hello, ello, ello world!
Note that two out of three instances were replaced. The first instance of “hello” however, was capitalized, and therefore ignored (there’s a solution to this in the operator example below).
Using the -replace operator
Alternatively, we could use the -replace operator:
$str = "Hello world!"
$str -replace "world", "Earth"
Hello Earth!
Whilst this still changes every instance of the substring that it finds, this time the parameter is case insensitive:
$str = "Hello, hello, hello world!"
$str -replace "hello", "ello"
ello, ello, ello world!
Using RegEx
One of the advantages of using the operator over the function, is that you can supply regular expressions to the first parameter of the replace operator,
For instance, if we wanted to replace all instances of the words “world”, “earth”, and “mars”, we could either perform the replace three times, or simply use the OR RegEx operator: |
$str = "Hello world!"
$str -replace "world|earth|mars", "unspecified planetary body"
Hello unspecified planetary body!
Case-Insensitive Replace
As shown above, the -replace operator is case-insensitive by default. This makes it the easiest way to do a case-insensitive replace in PowerShell:
$str = "Hello HELLO hello"
$str -replace "hello", "hey"
hey hey hey
If you actually want case-sensitive behavior with the operator, use -creplace instead:
$str = "Hello HELLO hello"
$str -creplace "hello", "hey"
Hello HELLO hey
The .replace() function, on the other hand, is always case-sensitive. There’s no built-in flag to change that, so if you need a case-insensitive replace with the function, you’re better off just using -replace.
Replacing Characters in a String
The same techniques work for replacing individual characters in a string. Swap out a single character:
$str = "one-two-three"
$str -replace "-", " "
one two three
Replace multiple different characters at once using a RegEx character class:
$str = "one-two_three"
$str -replace "[-_]", " "
one two three
You can also remove characters entirely by replacing them with an empty string:
$str = "Hello... world!!!"
$str -replace "[.!]", ""
Hello world
Replacing Text in a File
You can combine Get-Content and Set-Content with -replace to find and replace text inside a file:
(Get-Content -Path "config.txt") -replace "old-value", "new-value" |
Set-Content -Path "config.txt"
The parentheses around Get-Content are important — they force PowerShell to read the entire file before writing back to it. Without them, you’ll get a file lock error since you’d be reading and writing to the same file at the same time.