Skip to content

Instantly share code, notes, and snippets.

@adamrushuk
Last active July 29, 2025 17:02
Show Gist options
  • Save adamrushuk/34ad480bca761ad215a4adda73d448dc to your computer and use it in GitHub Desktop.
Save adamrushuk/34ad480bca761ad215a4adda73d448dc to your computer and use it in GitHub Desktop.

Revisions

  1. adamrushuk revised this gist Jul 29, 2018. 1 changed file with 55 additions and 0 deletions.
    55 changes: 55 additions & 0 deletions Get-GuidFromMsiFile.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    # Vars
    $msiPath = 'C:\Users\arush\Downloads\sqlncli.msi'

    # Definition
    $sig = @'
    [DllImport("msi.dll", CharSet = CharSet.Unicode, PreserveSig = true, SetLastError = true, ExactSpelling = true)]
    private static extern UInt32 MsiOpenPackageW(string szPackagePath, out IntPtr hProduct);
    [DllImport("msi.dll", CharSet = CharSet.Unicode, PreserveSig = true, SetLastError = true, ExactSpelling = true)]
    private static extern uint MsiCloseHandle(IntPtr hAny);
    [DllImport("msi.dll", CharSet = CharSet.Unicode, PreserveSig = true, SetLastError = true, ExactSpelling = true)]
    private static extern uint MsiGetPropertyW(IntPtr hAny, string name, StringBuilder buffer, ref int bufferLength);
    private static string GetPackageProperty(string msi, string property)
    {
    IntPtr MsiHandle = IntPtr.Zero;
    try
    {
    var res = MsiOpenPackageW(msi, out MsiHandle);
    if (res != 0)
    {
    return null;
    }
    int length = 256;
    var buffer = new StringBuilder(length);
    res = MsiGetPropertyW(MsiHandle, property, buffer, ref length);
    return buffer.ToString();
    }
    finally
    {
    if (MsiHandle != IntPtr.Zero)
    {
    MsiCloseHandle(MsiHandle);
    }
    }
    }
    public static string GetProductCode(string msi)
    {
    return GetPackageProperty(msi, "ProductCode");
    }
    public static string GetProductName(string msi)
    {
    return GetPackageProperty(msi, "ProductName");
    }
    '@
    $msiTools = Add-Type -PassThru -Namespace 'Microsoft.Windows.DesiredStateConfiguration.PackageResource' -Name 'MsiTools' -Using 'System.Text' -MemberDefinition $sig


    # Get the MSI Product Name
    $msiProductName = $msiTools::GetProductName($msiPath)
    Write-Host "[$msiProductName]" -ForegroundColor 'Yellow'
    ### Example Result: [Microsoft SQL Server 2012 Native Client ]

    # Get the MSI Product ID / GUID
    $msiProductGuid = $msiTools::GetProductCode($msiPath)
    Write-Host "$msiProductGuid" -ForegroundColor 'Yellow'
    ### Example Result: {49D665A2-4C2A-476E-9AB8-FCC425F526FC}
  2. adamrushuk created this gist Jul 29, 2018.
    8 changes: 8 additions & 0 deletions Get-ProductGuidRegistry.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    $x86Path = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
    $installedItemsX86 = Get-ItemProperty -Path $x86Path | Select-Object -Property PSChildName, DisplayName, DisplayVersion

    $x64Path = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
    $installedItemsX64 = Get-ItemProperty -Path $x64Path | Select-Object -Property PSChildName, DisplayName, DisplayVersion

    $installedItems = $installedItemsX86 + $installedItemsX64
    $installedItems | Where-Object -FilterScript { $null -ne $_.DisplayName } | Sort-Object -Property DisplayName | Out-GridView