Home » PowerShell » PowerShell – Replace text in a String [Examples]

PowerShell – Replace text in a String [Examples]

Very often in our day-to-day IT admin task, we may need to perform string operations on data to find a specific string, replace text in a string, and returns a new modified string.

In PowerShell, Replace() method and -replace operator is used to find specified characters and replace them with a new string.

Using Replace() method or replace operator, you can easily replace text in a string or replace a certain part of the string or entire string with new text in PowerShell.

In this post, I will explain to you how to perform PowerShell string replacement operation, string variable replacement, and multiple string replacements.

Using PowerShell Replace() Method

PowerShell Replace() method is used to replace old characters with new characters or text in a given string.

Syntax

.Replace(string oldvalue, string newvalue)

or

.Replace(char oldvalue, char newvalue)

Parameters

oldvalue – character/string to find

newvalue – character/string for replacement

PowerShell Tip: Tail – Get the last lines of a file in PowerShell!

Let’s understand, PowerShell Replace() method to replace text in a string with some examples as follows

PowerShell Replace String

Let’s consider, we have a string as below

ShellGeek

We want to replace characters in the ShellGeek string with new characters.

Characters to replace -> Shell

New Characters for replacement -> PowerShell-

Using PowerShell Replace() method to replace characters in a string, we can easily get expected results

"ShellGeel".Replace("Shell","PowerShell-")

The output of the above command after replacing characters Shell with PowerShell- is

PowerShell-Geek

Cool Tip: PowerShell String Concatenation with examples!

PowerShell Replace Characters in Variable

Let’s consider the scenario, we have a string stored in a variable and want to perform a PowerShell replace operation for a certain part of the text in a string.

PS C:\> $strmsg = "ShellGeek"

PS C:\> $strmsg.Replace("Shell","PowerShell-")
PowerShell-Geek

In the above example for a replacement string in a variable,

  • $strmsg variable holds the “ShellGeek” string value.
  • using Replace() method over variable $strmsg, it finds the “Shell” string and replaces it with “PowerShell-” text.
  • It gives output results as “PowerShell-Geek”

PowerShell Tip: Use Test Connection to ping a list of computers in PowerShell!

Multiple Replacements in a string using Replace()

To perform multiple replacements in a given string, we need to chain together it in one command.

PowerShell Replace() method returns modified string.

We can chain together and call to Replace() method to perform multiple replacements in a string.

All the methods in the command are executed from left to right.

Let’s consider, below example, where we have a computer name stored in a variable

PS C:\> $compname = "IT-100,IT-101,IT-102,IT-201,IT-202"

PS C:\> $compname.Replace("IT","Corp").Replace("10","L10").Replace("20","D20")
Corp-L100,Corp-L101,Corp-L102,Corp-D201,Corp-D202

In the above example,

  • $compname variable contains a list of all computer names
  • $compname.Replace(“IT”,”Corp”) – replace text ‘IT’ with ‘Corp’
  • $compname.Replace(“IT”,”Corp”).Replace(“10″,”L10”) – replace computername start with 10 with L10 ( L- Laptop)
  • $compname.Replace(“IT”,”Corp”).Replace(“10″,”L10”).Replace(“20″,”D20”) – replace computername start with 20 with D20 ( D- Desktop)
  • Multiple replacements in given string variable using multiple PowerShell Replace() method executed from left to right

Cool Tip: Do you know how to download a zip file in PowerShell!

PowerShell Replace character in a String

Using Replace() method to replace a character in a string is very easy and quick

PS C:\> $compname = "Corp-L10,Corp-L11,Corp-L12"

PS C:\> $compname.Replace('L','D')
Corp-D10,Corp-D11,Corp-D12

PS C:\> 

In the above example, $compname is having a computer name list.

The task is to find a computer name that starts with L and replaces it with D.

Using Replace(‘L’, ‘D’) is a very quick and easy way to replace all the occurrences of a particular character in a given string.

Cool Tip: Do you know how to add a newline to string or variable in PowerShell?

Using PowerShell replace operator

Syntax

-replace

replace operatorcase insensitive. Changes the left operand.

PowerShell Replace operator provides much flexibility as it uses regular expressions that match and replace expression patterns.

Let’s understand PowerShell replace operator with examples to perform advanced regular expression replacements for string.

Using PowerShell Replace operator for string word

Let’s consider a simple example to replace text in a string using replace operator.

PS C:\> $compname = "IT-100,IT-101,IT-102,IT-201,IT-202"

PS C:\> $compname -replace 'IT','Corp'
Corp-100,Corp-101,Corp-102,Corp-201,Corp-202

PS C:\> 

In the above example,

  • $compname variable stored computer name list
  • using -replace operator, find ‘IT’ and replace with ‘Corp’
  • $compname -replace ‘IT’,’Corp’ — replace characters ‘IT’ with text ‘Corp’ and returns the modified string.

PowerShell Tip: Use PowerShell $null to check if a variable is empty!

PowerShell Replace character in a String using -replace

Using the -replace operator to replace a single character in a given string.

PS C:\> $compname = "Corp-L10,Corp-L11,Corp-L12"

PS C:\> $compname -replace 'L','D'
Corp-D10,Corp-D11,Corp-D12

PS C:\> 

In the above example of character replacement using replace operator

  • $compname stores computer name list
  • -replace operator to find ‘L’ and replace with character ‘D’
  • Returns modified string

Cool Tip: Do you know how to print environment variables in PowerShell!

Multiple replacements using PowerShell replace operator

To perform multiple replacements in a given string, we need to chain together it in one command.

PowerShell replace operator returns modified string after string operation.

We can chain together and call to replace the operator to perform multiple replacements in a string.

All the methods in the command are executed from left to right.

Let’s consider, below example, where we have a computer name stored in a variable

PS C:\> $compname = "IT-100,IT-101,IT-102,IT-201,IT-202"

PS C:\> $compname -replace("IT","Corp") -replace("10","L10") -replace("20","D20")
Corp-L100,Corp-L101,Corp-L102,Corp-D201,Corp-D202

PS C:\> 

In the above example,

  • $compname variable contains a list of all computer names
  • $compname.Replace(“IT”,”Corp”) – replace text ‘IT’ with ‘Corp’
  • $compname.Replace(“IT”,”Corp”) -replace(“10″,”L10”) – replace computer name start with 10 with L10 ( L- Laptop)
  • $compname -replace(“IT”,”Corp”) -replace(“10″,”L10”) -replace(“20″,”D20”) —replace computer name start with 20 with D20 ( D- Desktop)
  • Multiple replacements in given string variable using multiple replace operator executed from left to right

Using Powershell Regex replace

replace operator provides more flexibility in using a regular expression (regex) to match and replace complex patterns.

Let’s understand simple examples to understand PowerShell regex replace functionality using replace operator.

Consider a scenario, we have a computer name stored in a variable and want to perform a replacement operation for a computer name that starts with ‘IT’ and ‘CT’ and replace with ‘Corp’

PS C:\> $compname = "IT-100,CT-101,IT-102,CT-201,IT-202"

PS C:\> $compname -replace 'IT|CT', 'Corp'
Corp-100,Corp-101,Corp-102,Corp-201,Corp-202

PS C:\> 

In the above example of Powershell script, using the regex with replace operator to replace the | in a string with a new string.

  • $compname stores computer name list
  • using -replace operator with regular expression regex OR (|) character, it searches for ‘IT’ or ‘CT’ characters in a given string
  • After a pattern match, it replaces the computer name starting with ‘IT’ or ‘CT’ with ‘Corp’

Using Escape to replace special characters (Replace Regex Characters)

While using PowerShell regular expression with replace operator, it has some pitfalls if regular expressions contain special characters or regex characters

Let’s consider an example, to replace [Shell] with PowerShell as given below

PS C:\> "[Shell] Geek" -replace '[Shell]','PowerShell'
[PowerShellPowerShellPowerShellPowerShellPowerShell] GPowerShellPowerShellk

PS C:\> 

In the above example, we want to replace [Shell] with PowerShell, the outcome of the above command is not as per what we want.

As the [Shell] contains regex special characters inside the search string, a correct replacement will not work.

To solve the above problem, there are two best options available

Using a backslash character at the front of the search character or using the escape() method

Let’s solve the above problem of replacing regex special characters in a string using the backslash character

PS C:\> "[Shell] Geek" -replace '\[Shell\]','PowerShell'
PowerShell Geek

PS C:\> 

In the above example, to avoid earlier discuss characters repeat problems because of PowerShell regex special character [ ].

In this case, using regular expression escape character to escape the square brackets in the above example.

However, another option to use escape() the method is the best way to escape all special characters

PS C:\> "[Shell] Geek" -replace ([regex]::Escape('[Shell]')),"PowerShell"
PowerShell Geek

PS C:\> 

Cool Tip: ErrorAction and ErrorVariable parameters in PowerShell!

Conclusion

In the above blog post, we have seen different ways to replace text in a string, multiple replacements in a given string using PowerShell Replace() method, and -replace operator

Use Replace() method for simple replacement of string.

However, if you want to perform an advanced replacement operation, use -replace operator with regular expression.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.