Skip to content

Instantly share code, notes, and snippets.

@mfcollins3
Last active December 10, 2015 14:29
Show Gist options
  • Save mfcollins3/4448042 to your computer and use it in GitHub Desktop.
Save mfcollins3/4448042 to your computer and use it in GitHub Desktop.
PowerShell function that will find a Windows Installer component in the registry and will delete the component.
# This function will accept component GUIDs from the pipeline
# and will attempt to find and delete the component records
# in the Windows Installer registry hive if they exist.
#
# For whatever reason, Windows Installer reverses the GUIDs
# when creating the component records in the registry. This
# function implements the logic for reversing the GUID values
# and building the correct registry key for each component.
function Remove-WindowsInstallerComponent {
$flags = [System.Text.RegularExpressions.RegexOptions]::Compiled -bor [System.Text.RegularExpressions.RegexOptions]::Singleline
$regex = new-object System.Text.RegularExpressions.Regex "\{?(?<a>[0-9A-F]{8})-(?<b>[0-9A-F]{4})-(?<c>[0-9A-F]{4})-(?<d>[0-9A-F]{4})-(?<e>[0-9A-F]{12})\}?",$flags
foreach ($componentId in $input) {
write-host "$componentId"
$match = $regex.Match($componentId)
if ($match.Success) {
$a = $match.Groups["a"].Value.ToCharArray()
$b = $match.Groups["b"].Value.ToCharArray()
$c = $match.Groups["c"].Value.ToCharArray()
$d = $match.Groups["d"].Value.ToCharArray()
[char[]]$d1 = $d[0..1]
[char[]]$d2 = $d[2..3]
$e = $match.Groups["e"].Value.ToCharArray()
[char[]]$e1 = $e[0..1]
[char[]]$e2 = $e[2..3]
[char[]]$e3 = $e[4..5]
[char[]]$e4 = $e[6..7]
[char[]]$e5 = $e[8..9]
[char[]]$e6 = $e[10..11]
[System.Array]::Reverse($a)
[System.Array]::Reverse($b)
[System.Array]::Reverse($c)
[System.Array]::Reverse($d1)
[System.Array]::Reverse($d2)
[System.Array]::Reverse($e1)
[System.Array]::Reverse($e2)
[System.Array]::Reverse($e3)
[System.Array]::Reverse($e4)
[System.Array]::Reverse($e5)
[System.Array]::Reverse($e6)
$d = [System.String]::Format("{0}{1}", (new-object System.String @(,$d1)), (new-object System.String @(,$d2)))
$e = [System.String]::Format("{0}{1}{2}{3}{4}{5}", (new-object System.String @(,$e1)), (new-object System.String @(,$e2)), (new-object System.String @(,$e3)), (new-object System.String @(,$e4)), (new-object System.String @(,$e5)), (new-object System.String @(,$e6)))
$a = new-object System.String @(,$a)
$b = new-object System.String @(,$b)
$c = new-object System.String @(,$c)
$d = new-object System.String @(,$d)
$e = new-object System.String @(,$e)
$key = "hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components\$a$b$c$d$e"
if (test-path $key) {
remove-item $key -Recurse -ErrorAction Continue
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment