How To Get The Uninstall Code (Product Code) From An MSI File With Powershell
Posted on February 14, 2025
- and tagged as
- powershell
When configuring Intune app deployments with MSI files there is the need to provide an Uninstall command. This is typically in the form of msiexec /x {Product Guid}
Extracting the MSI Product Code GUID with PowerShell
This string can be found in the registry once the app is installed, however, what if you want to avoid having to install it somewhere just to extract the product code?
Note, many online resources will tell you to query the Win32_Product
WMI Class. Don’t do that.
We can instead parse the MSI file with PowerShell and extract the product code.
function Get-MSIProductCode {
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$Path
)
$Pattern = 'ProductCode(\{[0-9a-fA-F-]{36}\})'
(Get-Content $Path -Encoding ASCII -ReadCount 100 | sls $Pattern).Matches.Groups[1].Value
}
Here’s the output:
PS > Get-MSIProductCode C:\temp\azure-cosmosdb-emulator-2.14.16-343dd460.msi
{2D79E7EA-307E-4129-AE7B-DCE93FEC31D8}
This is simple and works well for small files, but even for our moderately sized azure-cosmosdb-emulator example, which is ~230 MB, it’s a little slow.
PS > Measure-Command {Get-MSIProductCode C:\temp\azure-cosmosdb-emulator-2.14.16-343dd460.msi} | select -exp TotalSeconds
4.0902182
So here is a faster version using a .NET StreamReader:
function Get-MSIProductCode {
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$Path
)
$Pattern = 'ProductCode(\{[0-9a-fA-F-]{36}\})'
$Stream = [System.IO.File]::OpenRead($Path)
$Reader = [System.IO.StreamReader]::New($Stream, [System.Text.Encoding]::ASCII)
while ($null -ne ($Line = $Reader.ReadLine())) {
if ($Line -match $Pattern) {
$Matches[1]
break
}
}
$Reader.Close()
}
We get the same result:
PS > Get-MSIProductCode C:\temp\azure-cosmosdb-emulator-2.14.16-343dd460.msi
{2D79E7EA-307E-4129-AE7B-DCE93FEC31D8}
In around half the time:
PS > Measure-Command {Get-MSIProductCode C:\temp\azure-cosmosdb-emulator-2.14.16-343dd460.msi} | select -exp TotalSeconds
1.8438104
Using a Hex editor to find MSI Product Code
Lastly, if you’re not PowerShell inclined, you can also open the MSI file in a Hex editor and search for “ProductCode”. Here I’m using HxD: