0

I feel like I'm missing something obvious but my mind's gone blank.....

I have the following output

PS C:\Users\User1> $TargetComputers

ComputerName   UninstallString                                                                               
------------   ---------------                                                                               
Server1        MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}                                          
Server2        MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}                                          
Server3        MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}                                          
Server4        C:\Program Files\7-Zip\Uninstall.exe                                                          
Server5        MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}                                          
Server6        MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}                                          
Server7        {"D:\Program Files\7-Zip\Uninstall.exe", MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}}
Server8        MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}                                          
Server9        MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}  

How do I manipulate the UninstallString column to leave me just {GUID}, including literal path where specified, when I call $TargetComputers?

1
  • for most of those line, split on the {, take the 2nd part, split on the } and take the 1st part. for the lines that don't start with 'MsiExec.exe`, you will need to adjust what part you take after the splits. Commented Jan 28, 2021 at 19:10

2 Answers 2

2
# Sample data
$TargetComputers = @(
    @{ ComputerName='server1'; UninstallString='MsiExec.exe /I{A7E794A1-D6E9-43CC-9B69-DEB6B5A91EF9}' }
    @{ ComputerName='server1'; UninstallString='{"D:\Program Files\7-Zip\Uninstall.exe", MsiExec.exe /I{36648B37-EA03-4349-8C49-C26032D06C61}}' }
    @{ ComputerName='server1'; UninstallString='C:\Program Files\7-Zip\Uninstall.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 Files\7-Zip\Uninstall.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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this and the detailed explanation.
0

Try with:

$TargetComputers = $TargetComputers | ForEach-Object {
  $matches = [Regex]::Matches($_.UninstallString, ({.*}),"IgnoreCase")
  if ( $null -ne $matches.Groups ) {
    $_.UninstallString = $matches.Groups[1].Value
  }

  return $_
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.