July 2018
Beginner
552 pages
13h 18m
English
The -replace operator will work very similarly to the -match operator, and allows for the systematic replacement of strings using regular expressions:
# Simple usage of -replace'PowerShell is hard.' -replace 'hard', 'easy' # 'PowerShell is easy.'
This statement searches for the hard statement and replaces it with easy. In addition, you can use dedicated regular expressions and replace many values, as shown in the following example:
# Using Regex to switch words"PowerShell Awesomeness" -replace '(.*) (.*)','$2 $1' # 'Awesomeness PowerShell'
To understand this line of code, let's use our previously learned -match statement:
# Explaining the example with -match"PowerShell Awesomeness" -match '(.*) (.*)'$Matches# Output of ...
Read now
Unlock full access