PowerTip: Use PowerShell to Remove Characters in a String


Summary: Learn how to use Windows PowerShell to remove specific characters in a string.

Hey, Scripting Guy! Question I have a string that includes braces:

$a = ‘{Abcde}’

   I want to remove the braces, but keep the text string inside the braces.
   How can I do this by using Windows PowerShell?

Hey, Scripting Guy! Answer Use the –Replace operator, create a regular expression pattern that includes only the braces you want to remove,
           and then write the results back to the variable, for example:

PS C:> $a = ‘{Abcde}’

PS C:> $a -replace ‘[{}]’,”

abcde

PS C:> $a = $a -replace ‘[{}]’,”

PS C:> $a

abcde

Note  This command uses single, back-to-back quotation marks .

Comments (3)

  1. BubbleGum says:

    I use such construction
    $a = "{abcde}"
    $a -replace ‘$a[0]’

  2. Astro says:

    Hi,

    Any tips on how to do this on a specific match? I only want to do the above to the portion of the line that contains numbers.

Skip to main content