Converting VBScript's Replace Function

Definition: Returns a string in which a specified substring has been replaced with another substring a specified number of times.

Replace

The world as we know it would come to a crashing halt if we didn’t have search-and-replace commands. In Windows PowerShell you can do a simple search-and-replace on any string value simply by using the -replace parameter and specifying two things:

  • The string value to search for.

  • The replacement text.

For example, the following two command assign a value to the variable $a, then use -replace to replace all instances of the letter x with the letter a:

$a = "bxnxnx"
$a = $a -replace("x","a")

When you run this command and then echo back the value of $a you should get the following:

banana

Return to the VBScript to Windows PowerShell home page