Search notes:

PowerShell operator -Replace

The PowerShell operator -replace replaces a regular expression in a struing with another string:
PS C:\> 'foo42bar999baz' -replace '\d+', ' - '
foo - bar - baz

Remove comments from a text

In order to remove comments starting with # up to the end of a line, multi line mode must be turned on with (?m):
PS C:\> @'
>> one
>> two # comment
>> three
>> four # another comment
>> five
>> '@ -replace '(?m)#.*$', ''
one
two
three
four
five

Replacing the values of psObject properties

$objs  = `
     ( new-object psObject -property ( [ordered]@{ val_1 = 'one'   ; val_2 = 'two'   ; val_3 = 'three' } ) ) ,
     ( new-object psObject -property ( [ordered]@{ val_1 = 'foo'   ; val_2 = 'bar'   ; val_3 = 'baz'   } ) ) ,
     ( new-object psObject -property ( [ordered]@{ val_1 = 'abcde' ; val_2 = 'fghij' ; val_3 = $null   } ) ) ;

$objs | foreach-object {
   $_.val_2 = $_.val_2 -replace 'o', 'X'
   $_
}

#
# val_1 val_2 val_3
# ----- ----- -----
# one   twX   three
# foo   bar   baz
# abcde fghij
Github repository about-PowerShell, path: /language/operator/string-manipulation/replace/psObjects.ps1

Index