Using the PowerShell replace operator,creplace or replace() method, we can easily replace the first occurrence of a word in a string.
PowerShell string has many built-in functions to manipulate string, like replace() method to replace the word in a string, split the string, etc…
In this article, we will discuss how to replace the first occurrence of a string in a string using the PowerShell replace operator, replace() method.
PowerShell Replace First Occurrence of String using replace Operator
PowerShell replace operator uses the Regular expressions (regex) to search for text. We can use the regex expression in replace operator to replace the first occurrence of a string in a text.
Let’s say that, we have a string stored in the variable $str = “Hello Admin! Let’s learn PowerShell with Hello World program.” We want to replace the first instance of Hello with Hi
To replace the first occurrences of the word in a string using the replace operator, refer to the following code.
$str = "Hello Admin! Let's learn PowerShell with Hello World program." $str -replace '^(.*?)Hello','$1Hi'
In the above PowerShell script, the $str variable stores the string data. To replace the first instance of word Hello with Hi ,use the replace operator.
The replace operator uses the regex expression, anchor ^ asserts the position at start of the string, and (.*?) for the first capturing group for Hello and second parameter is string Hi to replace with found text.
The output of the above PowerShell script to replace the first instance of text in a string is:
Cool Tip: How to replace newline with a comma in PowerShell!
Use creplace to Replace First Instance of String in PowerShell
Using the PowerShell creplace operator, you can replace the first occurrence of a string in PowerShell.
creplace operator is case sensitive used to replace the string in PowerShell.
$str = "Hello Admin! Let's learn PowerShell with Hello World program." $str -creplace "Hello",'Hi'
In the above PowerShell script, the $str variable stores the string data. Use the creplace operator which takes two arguments; string to search for and string to replace with found string.
The output of the above script replaces the first occurrences of the string Hello with Hi
PS C:\> $str = "Hello Admin! Let's learn PowerShell with Hello World program."
PS C:\> $str -creplace "Hello",'Hi'
Hi Admin! Let's learn PowerShell with Hi World program.
PS C:\>
Cool Tip: How to replace a string in multiple files in PowerShell!
Replace First Occurrence of String using PowerShell Replace() method
Use the PowerShell replace() method to replace the first occurrence of a string in a string.
Let’s consider the above example of the $str variable which stores the string data.
$str = "Hello Admin! Let's learn PowerShell with Hello World program." [regex]$pattern = "Hello" $pattern.replace($str, "Hi", 1)
In the above PowerShell script, the $str variable stores the string.
[regex]$pattern = “Hello” applies conversion of string to regex and uses the replace() method over the $pattern to specify the input string Hello, replacement string Hi, and count to replace the string.
The output of the above script after the replacement of the first instance of string in the text is:
PS C:\> $str = "Hello Admin! Let's learn PowerShell with Hello World program."
PS C:\> [regex]$pattern = "Hello"
PS C:\> $pattern.replace($str, "Hi", 1)
Hi Admin! Let's learn PowerShell with Hello World program.
PS C:\>
Conclusion
I hope the above article on how to replace the first occurrences of a string in text using the PowerShell replace operator, creplace, or replace() method is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.