Tuesday, 17 February 2015

PowerShell to Remove Characters in a String

How to remove characters in a string using powershell

Lets start of with examples, and we have to use the –Replace operator, create a regular expression pattern that includes what you want to remove and then write the results back to the variable.

Note: here we are over writing the variable with the replaced characters.

$string = '{Abcde123**$45}'

$string =  $string -replace '[{}]','' # this will remove the {}

$string = $string -replace '12','' # this will remove 12

$string = $string -replace '12','{}' # this will replace 12 with {}

$string = $string -replace '\$','' # this would remove the $ sign Note: the \ before the $ after replace command.

Lets try one by one in PowerShell.





No comments:

Post a Comment