Powershell

How to Use PowerShell replace to Replace Text With Examples

Replacing multiple texts or strings in a program one by one is a complex and time-consuming process. This process can be simplified by using PowerShell. More specifically, PowerShell offers the “Replace()” method and the “-replace” operator to replace multiple instances of texts, strings, and characters at once with other data.

This write-up will discuss the approaches to replace text in PowerShell.

How to Use PowerShell Replace to Replace Text?

In PowerShell, text can be replaced using the below-provided approaches:

Method 1: Replace Text in PowerShell Using the “Replace()” Method

The PowerShell “replace()” method assists in replacing the characters in a text. This method accepts arguments, the first argument finds the string, and the second argument replaces the found string.

Example 1: Replace Text With a Substring in a String by Using the “Replace()” Function

This example will demonstrate the method to replace a text using the “replace()” method:

> $text = "Hi John, How are you?"

> $text.replace("Hi John", "Hi David")

According to the above code:

  • First, assign two strings within inverted commas to a variable “$text”.
  • After that, concatenate the string-assigned variable with the “replace()” method.
  • Inside the replace method, we have added two arguments. The first argument will get replaced with the second one:

It can be observed that the “Hi John” has been replaced with the “Hi David” string.

Example 2: Replace Text With a Space in a String by Utilizing the “Replace()” Function/Method

Now, replace the text with space by passing ” ” as the second parameter in the “replace()” method:

> $text = "Hi John, How are you?"

> $text.replace("Hi John", " ")

In this code example:

  • We have passed the two arguments inside the “replace()” method.
  • The first argument will find the string to replace, and the second string will replace the string.
  • We have left the second argument empty so that text gets replaced with an empty string in order to delete the partial text string:

It can be noticed that the specified substring has been replaced with space.

Method 2: Replace Text in PowerShell Using “-replace” Operator

The replace operator is denoted with a hyphen at the start, such as “-replace”. Its operation is quite similar to the “Replace()” method. However, it can replace the instances of regular expressions (regex).

Example 1: Replace Text With a Substring in a String by Utilizing the “-replace” Operator

Now, run the following commands in the PowerShell console:

> $text = "Good Morning, John Doe"

> $text -replace "Good Morning", "Good Night"

In the above-stated code:

  • First, access the created string and add the “-replace” operator.
  • Then, assign the two regular expressions or substring after the mentioned to the operator.
  • First regular expression will find the text to be replaced, while the second regular expression will replace the text:

It can be noticed that the text has been replaced using the “-replace” operator.

Example 2: Replace Text With a Space in a String by Utilizing the “-replace” Operator

In this example, we will demonstrate the removal of text with the space using the “-replace” operator:

> $text = "Good Morning, John Doe"

> $text -replace "Good Morning", " "

In this code, we have added the empty regular expression so that it can remove the specified text:

That was all about replacing text in PowerShell.

Conclusion

The text in PowerShell can be replaced using two methods, including the “Replace()” method and the “-replace” operator. First of all, they retrieve the respective string or text and then replace it. Moreover, you can also utilize these approaches to replace the characters of the text or string with empty spaces. This write-up has delivered a detailed guide to replacing text in PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.