How to get MS Office product key

rated by 0 users
This post has 4 Replies | 2 Followers

Top 50 Contributor
Posts 35
Top Contributor
p3dr0 Posted: 05-18-2009 2:06 AM

Hello all, I would like to ask if is possible to get MS Office Product key from registry using powershell.

I have one script which gets Windows Product key I need same think for office. Is it possible ?

Please help.

 

Peter.

Top 10 Contributor
Posts 248
Microsoft MVP
Top Contributor

This code will give you MS Office product key (no matter if you have 32-bit or 64-bit version of Office XP, Office 2003, or Office 2007).

function Search-RegistryKeyValues {
 param(
 [string]$path,
 [string]$valueName
 )
 Get-ChildItem $path -recurse -ea SilentlyContinue |
 % {
  if ((Get-ItemProperty -Path $_.PsPath -ea SilentlyContinue) -match $valueName)
  {
   $_.PsPath
  }
 }
}

# find registry key that has value "digitalproductid"
# 32-bit versions
$key = Search-RegistryKeyValues "hklm:\software\microsoft\office" "digitalproductid"
if ($key -eq $null) {
    # 64-bit versions
 $key = Search-RegistryKeyValues "hklm:\software\Wow6432Node\microsoft\office" "digitalproductid"
 if ($key -eq $null) {Write-Host "MS Office is not installed.";break}
}

$valueData = (Get-ItemProperty $key).digitalproductid[52..66]

# decrypt base24 encoded binary data
$productKey = ""
$chars = "BCDFGHJKMPQRTVWXY2346789"
for ($i = 24; $i -ge 0; $i--) {
 $r = 0
 for ($j = 14; $j -ge 0; $j--) {
  $r = ($r * 256) -bxor $valueData[$j]
  $valueData[$j] = [math]::Truncate($r / 24)
  $r = $r % 24
 }
 $productKey = $chars[$r] + $productKey
 if (($i % 5) -eq 0 -and $i -ne 0) {
  $productKey = "-" + $productKey
 }
}

Write-Host "MS Office Product Key:" $productKey

Top 50 Contributor
Posts 35
Top Contributor

Thanks, yes it works, but I have one more question, if is possible to search registry for some key but remotely.

Top 10 Contributor
Posts 248
Microsoft MVP
Top Contributor
This script will give you a nice object with ComputerName, ProductName (tested on Office and Visio), and ProductKey.
Works against the local and a remote computer. You can pass an array of computer names.
function Get-MSOfficeProductKey {
    param(
    [string[]]$computerName = "."
    )

    $product = @()
    $hklm = 2147483650
    $path = "SOFTWARE\Microsoft\Office"

    foreach ($computer in $computerName) {

        $wmi = [WMIClass]"\\$computer\root\default:stdRegProv"

        $subkeys1 = $wmi.EnumKey($hklm,$path)
        foreach ($subkey1 in $subkeys1.snames) {
            $subkeys2 = $wmi.EnumKey($hklm,"$path\$subkey1")
            foreach ($subkey2 in $subkeys2.snames) {
                $subkeys3 = $wmi.EnumKey($hklm,"$path\$subkey1\$subkey2")
                foreach ($subkey3 in $subkeys3.snames) {
                    $subkeys4 = $wmi.EnumValues($hklm,"$path\$subkey1\$subkey2\$subkey3")
                    foreach ($subkey4 in $subkeys4.snames) {
                        if ($subkey4 -eq "digitalproductid") {
                            $temp = "" | select ComputerName,ProductName,ProductKey
                            $temp.ComputerName = $computer
                            $productName = $wmi.GetStringValue($hklm,"$path\$subkey1\$subkey2\$subkey3","productname")
                            $temp.ProductName = $productName.sValue

                            $data = $wmi.GetBinaryValue($hklm,"$path\$subkey1\$subkey2\$subkey3","digitalproductid")
                            $valueData = ($data.uValue)[52..66]

                            # decrypt base24 encoded binary data 
                            $productKey = ""
                            $chars = "BCDFGHJKMPQRTVWXY2346789"
                            for ($i = 24; $i -ge 0; $i--) { 
                                $r = 0 
                                for ($j = 14; $j -ge 0; $j--) { 
                                    $r = ($r * 256) -bxor $valueData[$j] 
                                    $valueData[$j] = [math]::Truncate($r / 24)
                                    $r = $r % 24 
                                } 
                                $productKey = $chars[$r] + $productKey 
                                if (($i % 5) -eq 0 -and $i -ne 0) { 
                                    $productKey = "-" + $productKey 
                                } 
                            } 
                            $temp.ProductKey = $productKey
                            $product += $temp
                        }
                    }
                }
            }
        }
    }
    $product
}

# Example:
# Get-MSOfficeProductKey Serv01,Serv02 | Format-Table * -auto
# Get-MSOfficeProductKey -computerName (Get-Content servers.txt)
Top 50 Contributor
Posts 35
Top Contributor

Really great many thanks.

Page 1 of 1 (5 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.