Home » PowerShell » PowerShell Replace – How to Use to Replace String

PowerShell Replace – How to Use to Replace String

PowerShell String stores the string data and provides various methods to handle string manipulation. Replace string or characters in a string or file is one such string manipulation. PowerShell has a replace() method to achieve the replacement of string.

In this article, we will discuss how to use PowerShell replace() method over the PowerShell string and replace characters in the string.

PowerShell Replace() Method

replace() method in PowerShell replace substring in a string. It finds the old value and replaces it with the new value.

Syntax

string Replace(char oldValue, char newValue)

string Replace(string oldValue, string newValue)

Parameters:

oldValue: character or string to find in a string.

newValue: character or string to replace the found text.

Output:

It returns the string after the replacement of the string.

PowerShell replace character in a string using replace()

Using the PowerShell replace() method, you can easily replace the character with a new value.

PowerShell replace() method has two arguments, string or character to find and string to replace the find text with.

$str = "Welcome!Admin to ShellGeek."  

 $str.Replace('!',',')   

In the above PowerShell script, the $str variable stores the string data. To replace a character ! in a given string with a comma, call the replace() method over the $str variable.

replace() method takes two arguments, char ! to find in a given string and char, to replace with find character in PowerShell string.

The output of the above PowerShell script to replace a character in a string is:

PowerShell replace character
PowerShell replace character

Replace string in PowerShell using replace()

You can use the PowerShell string replace() function to replace the text in a string. It accepts two arguments a string to find and a string to replace with found text.

$str = "This is example about PowerShell string split method" 
 
$str.Replace("split","replace")   

In the above PowerShell script, the $str variable stores the string data. To replace the text “split” in a given string with “replace“, call the PowerShell replace() method.

It replaces the text with a new string value and returns the string as given below

PS C:\> $str = "This is example about PowerShell string split method"                                                   

PS C:\> $str.Replace("split","replace")                                                                                 

This is example about PowerShell string replace method

PS C:\>   

Conclusion

I hope the above article on how to use the PowerShell string replace() method to replace characters or a string in a string is helpful to you.

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

Recommended Content

PowerShell Replace Text in a String

PowerShell Replace Substring

PowerShell Replace Multiple Characters in a String

PowerShell Replace Special Character

PowerShell Replace Line in a File

PowerShell Replace with Wildcard

PowerShell Replace Newline with Comma

PowerShell Replace String in Multiple Files

PowerShell Replace First Occurrence of String

PowerShell Replace First Character in String

PowerShell Replace Single Quotes in String

PowerShell Replace Space with Newline in String

PowerShell Replace Double Quotes in String

PowerShell Replace Parentheses in String

PowerShell Replace Pipe Character in String

PowerShell Replace Case Insensitive in String

PowerShell Replace Space with Comma in String

PowerShell Replace Variable in a File

PowerShell Replace String in Array