Updated February 28, 2023
Introduction to PowerShell String Replace
Manipulation of strings in an integral part of any programming language. String functions are an integral part of PowerShell and there are various functions present to handle the string manipulation related tasks. In PowerShell, everything is an object and string is also an object of type System.String. To remove certain part of a string or replace a part or an entire string with some other text can be achieved using the replace cmdlet in PowerShell. The replace function takes the text to be replaced in a string as a parameter and the text with which the desired text must be replaced as another parameter. This article will cover in detail the replace cmdlet in detail along with examples.
Syntax:
Replace(strOldChar, strNewChar)
- Stroldchar: Character to be found
- Strnewchar: character to be replace the found text.
Examples to Implement PowerShell String Replace
Below are the examples of PowerShell String Replace:
Example #1
Code:
Write-Host "replacing a text in string"
$test=" old text to be replaced"
Write-Host "going to replace old"
$test.Replace("old","New")
Write-Host "Text is replaced"
Output:
Example #2
Code:
Write-Host "Welcome to Replace function demo in PowerShell"
Write-Host "Demo of single text replacement"
$input="Vignesh Krishnakumar"
Write-Host "Actual text before replace is" $input
Write-Host "The below code will replace vignesh with Viki"
#replace function
$input=$input -replace "vignesh","Viki"
Write-Host "Replaced text is" $input
Write-Host "Replace using replace function"
$input1="this is a nice day"
Write-Host "The actual text is" $input1
Write-Host "The below will replace this with that"
#replace function
$input1=$input1.Replace("this","That")
Write-Host "After replace" $input1
Write-Host "Demo of multiple replace simultaneosly"
$actual="Australia won the world cup in 1999"
#multiple replacement
$actual -replace "Australia","1999" -replace "lost", "-"
Output:
Replace Using Hash table
The other effective way of replacing multiple text simultaneously would be to use hash table. Hash table is a type of an array, which stores values as key value pair. The key values must be unique, and values can be non-unique. The built-in properties of a hash table are key, value and count. In the case of replacing text using hash table, the key would represent the text to be replaced and the value would represent the text value to be used for replacement.
Syntax:
$hashtable=@
{
Text1=value1;
Text2=value2;
Text3=value3;
Text4=value4
}
The values of a hash table can be altered as follows.
- To add a new pair to hash table:
$hashtable.Add(“text5”,”value5”)
- To edit a value:
$hashtable.Set_Item(“text5”,”value6”)
Or
$hastable.”text5”=”value6”
- To remove a value:
$hashtable.Remove(“text5”)
- To display hash table:
$hashtable
Example #3
Code:
Write-Host "Welcome to demo of replace using hashtable"
$input="Am from chennai. I work as a freelancer. This is my hobby. This is an example. Let us see how text are replaced"
Write-Host "Text before replacing `n" $input -ForegroundColor Yellow
$hashtable = @{}
$hashtable.'Am'='We'
$hashtable.'This'='That'
$hashtable.'is'='was'
$hashtable.'us'='we'
$hashtable.'.'='!'
Write-Host "The hashtable keys are as follows `n" $hashtable.Keys -ForegroundColor Yellow
Write-Host "The hashtable values are as follows `n" $hashtable.Values -ForegroundColor Yellow
#iterate the input string and replace the values
Foreach ($key in $hashtable.Keys) {
$input = $input.Replace($key, $hashtable.$key)
}
Write-Host "After replacement" $input -ForegroundColor Yellow
Write-Host "Demo of replacing numbers and special characters"
$input1="this 1 35 78 90. Example of replacing numbers. % * # $. 12 34 56 78"
Write-Host "Text before replacing `n" $input1 -ForegroundColor Yellow
$hashtable1 = @{}
$hashtable1.'35'='350'
$hashtable1.'78'='780'
$hashtable1.'90'='900'
$hashtable1.'%'='replced'
$hashtable1.'*'='sfdsfdsfds'
$hashtable1.'#'='####'
Foreach ($key in $hashtable1.Keys) {
$input1 = $input1.Replace($key, $hashtable1.$key)
}
Write-Host "After replacement" $input1 -ForegroundColor Yellow
Output:
Example #4
Code:
Write-Host "Replacing a multiple text"
$string="my name is billa, vazhaikai elam naanum super"
$string-replace "vazhikai", "replaced" -replace "elam", "All"
Write-Host "Replacing a text in a file"
(Get-Content -path C:\Users\educba.txt -Raw) -replace 'was','is'
Write-Host "text in the file is replaced"
Output:
Example #5
Code:
Write-Host "Example of replacing a text in a file"
$filePath = 'C:\Users\educba.txt'
$find='this'
$replace='that'
$file=Get-Content -Path $filePath
Write-Host "The contents of the file are `n" $file -ForegroundColor Yellow
(Get-Content $filePath) -replace $Find, $Replace | Add-Content -Path $filePath
$newtext=Get-Content -Path $filePath
Write-Host "Now the text in the file is" $newtext -ForegroundColor Yellow
Output:
Example #6
Code:
Write-Host "Welcome to string comparison"
$test=" STRINGsdfsdfdsfdsf DEMO"
$test1="stringDDDDDDDD demo"
Write-Host "Comaprison using equal method"
$test.Equals($test1)
$test.ToLower.Equals($test1)
$test.Equals($test1,1)
Write-Host "Comaprison using comaprison method"
$test.CompareTo($test1)
Output:
Example #7
Code:
Write-Host "Replacing using regex expression"
$input="192.10.145.30.456"
Write-Host "Input is `n" $input -ForegroundColor Yellow
Write-Host "Using regex going to replace all trwo digits with **"
$input=$input -replace "\.\d{2}\.","**"
Write-Host "After replacing `n" $input -ForegroundColor Yellow
Write-Host "Example to show interchanging of names"
$input= "Vignesh Krishnakumar"
Write-Host "Going to replace using regex and swap"
$input= $input -replace "([a-z]+)\s([a-z]+)",'$2, $1'
Write-Host "after swap `n" $input -ForegroundColor Yellow
Output:
Example #8
Other Commonly Used String Functions:
.Split()
This is another method that can be used to split a string into substrings.
Syntax:
.Split(strSeparator [, MaxSubstrings] [, Options])
String -Split strSeparator [, MaxSubstrings] [, Options]
String -Split {scriptblock} [, MaxSubstrings]
-Split String
strSeparator: It is character of identification to split the string
MaxSubstrings: The maximum number of substrings that can be generated
Code:
Write-Host "generating substring using split method"
$teststring="my name is vignesh- am from chennai"
Write-Host "splitting using - character"
$teststring -split "-"
$teststring="domainname\username"
Write-Host "Splitting using \ character"
$teststring -split "\\"
Write-Host "generating substring using space"
$string="string1 string2 strin3"
$string.Split("")
Write-Host "splitting using multiple separators"
$string="domain\systems-test"
$string.Split("\\").Split("-")
Output:
Conclusion
Thus, the article covered in detail the string replace function in PowerShell. The article explained the various ways in which the string can be replaced. It covered in detail with example how multiple text can be replaced simultaneously, how hash table can be used to replace text, how string in a file can be replaced with appropriate examples. It also explained in detail how regular expressions can be used to replace text in a string. It also covered about split method in PowerShell. The article also explained how multiple values can be stored in a hash table and how it can be used for replacing text in a string. The best way to learn more about this would be to try other various methods and practice them in sample scripts.
Recommended Articles
This is a guide to PowerShell String Replace. Here we discuss the Introduction to the PowerShell String Replace and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –