# Sample data
$TargetComputers = @(
@{ ComputerName='server1'; UninstallString='MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}' }
@{ ComputerName='server1'; UninstallString='{"D:Program Files7-ZipUninstall.exe", MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}}' }
@{ ComputerName='server1'; UninstallString='C:Program Files7-ZipUninstall.exe' }
) | % { [PSCustomObject] $_ }
$TargetComputers | ForEach-Object {
$_.UninstallString = $_.UninstallString -replace '.*/I{(.+?)}.*', '$1'
}
$TargetComputers
Output:
ComputerName UninstallString
------------ ---------------
server1 A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9
server1 36648B37-EA03-4349-8C49-C26032D06C61
server1 C:Program Files7-ZipUninstall.exe
Explanation:
- By using the
-replace
operator, we are using regular expression (RegEx) pattern matching. The 1st string after -replace
is the search string, the 2nd string is the substitute. If the RegEx doesn't match, the original string will be returned.
- RegEx search string breakdown:
.*
- any character, zero or more times
/I{
- literal sub string
(
- starts a capture group
.+?
- any character, one or more times, as little as possible
)
- ends the capture group
}
- literal sub string
.*
- any character, zero or more times
- RegEx substitute:
$1
- replaces the string with the content of the 1st capture group (the GUID value)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…