PowerTip: Replace Multiple Words in String with PowerShell


Summary: Use Windows PowerShell to replace multiple words in a string.  

Hey, Scripting Guy! Question How can I use Windows PowerShell to replace misspelled words in a string?

Hey, Scripting Guy! Answer Use the –Replace operator, for example:

$s = "The calander says there are five calendar days left in the callander"

$s -replace "calander|callander","Calendar"

Comments (4)

  1. Greg Wojan says:

    It’s a regular expression…

  2. The pipe can be used to seperate out words, or is that some form of RegEx, etc?

  3. Byron White says:

    Can anyone tell me why
    $s -replace "calander|callander","Calendar"
    The Calendar says there are five calendar days left in the Calendar

    behaves differently than
    $s.replace("calander|callander","Calendar")
    The calander says there are five calendar days left in the callander

    As far as I understand it, they are functionally equivalent, but they’re obviously not!

  4. runBy says:

    Oops, forgot to log in first.

    Byron

Skip to main content