Powershell

PowerShell replace operator | Explained

The PowerShell provides the advanced features of comparison operators. These operators include equality, matching, containment, type and replacement operators. The PowerShell comparison operator is basically used for comparing the two specified values of expressions. In this article, we will explain the replace operator of PowerShell.

The main objective of this article is to explain the PowerShell replace operator.

How to use PowerShell replace operator?

The PowerShell replace operator is denoted by “-replace” which is used for updating or substituting the specified value using regular expressions. With replace operate you can also rename the files and even can change their extensions.

In PowerShell replace operator, there are three main components. These components include -replace, -creplace, and -ireplace. All these replace operator components will be practically implemented in the upcoming articles.

Example 1: How to replace a single value in PowerShell?

The -replace operator is used to substitute a specified value of the regular expression. For instance, the example code matches the “b” and replaces it with the “B”.

'a, b, c, d' -replace 'b' , 'B'

In the above output, it can be seen that “b” in lowercase is replaced with uppercase “B”.

Note: The first value after -replace is search expression while the second is replace/substitute expression.

Example 2: How to replace multiple values in PowerShell?

The following command is working the same as above. However, the “-replace” operator is used to replace “a” and “b” with “A” and “B” respectively.

'a, b, c, d' -replace 'a, b' , 'A, B'

The output represents that the “-replace” operator has replaced the letters “a” and “b” with “A” and “B” respectively.

Example 3: How to update string values using PowerShell?

In this example, we will use the replace operator to update the string values. The following code stores four values in a variable.

$fruit = 'Apple, Mango, Banana, Peach'

Now, use the replace operator to update the specific value. For instance, I want to update the ‘Peach’ with ‘Orange’. The following command will replace the Peach with Orange.

$fruit -replace 'Peach' , 'Orange'

As practically illustrated, the output returned the updated value regardless of the old value (Peach).

This time we will store the fruit items in the $items variable. We have also added the status of the fruit items, whether it is available or not.

$items = 'Apple:yes, Mango:yes, Banana:yes, Peach:yes'

As you can see, the fruit items are unavailable with ‘Yes’ status.

This time I need to update the status of Mango from ‘Yes’ to ‘No’. Run the below-mentioned command using replace operator.

$items -replace 'Mango:yes' , 'Mango:no'

It can be observed in the output that Mango is now updated with the status ‘No’.

Just in case I want to update all the values from the Yes to No, then the following command will be used.

$items -replace 'yes' , 'no'

The output shows that the status of all fruit items is ‘No’.

Example 4: How to replace the case-sensitive expression?

The replace operator is case insensitive by default. But you can make it case-sensitive using the “-creplace” operator. You can apply the case-sensitivity rules by adding “c” with the -replace operator. For instance, run the following command to replace the lowercase “ a ” with “ * ”.

$fruit -creplace "a", "*"

As per the above output screenshot, all the “a” is substituted with “ * ”

Here is another example, this time, we will substitute the “A” with “ * ”. Run the below-mentioned example command.

$fruit -creplace "A", "*"

The result can be seen that the command found only a single “A” and replaced it with “ * ”.

Example 5: How to replace the case-insensitive expression?

The “-ireplace” can also be used to make the expression explicitly case-insensitive. The case-insensitive operator will update all the matching letters/words/values regardless of the case sensitivity.

$fruit -ireplace "A", "*"

According to the above output screenshot, all the letters both in upper and lowercase have been updated with “ * ”.

Here you go! Using the replace operator, you have learned to replace or substitute the values in PowerShell.

Conclusion

PowerShell replace operator can substitute and rename the specified values of the regular expressions. Replace operator comes under the comparison operator of PowerShell. In this article, you have learned the essential functions and purpose of the PowerShell replace operator. For a better understanding, we have demonstrated examples to show the practical implication of the replace operator in PowerShell.

About the author

Adnan Shabbir