zetzke
zetzke

Reputation: 101

REG_BINARY to Windows-Key in PowerShell

I am trying to extract a "readable" win10-key from a device, where the key is digital.

Meaning : (Get-WmiObject -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey gives no result.

I use reg query HKLM\SOFTWARE\Microsoft\"Windows NT"\CurrentVersion /v DigitalProductId but ofcourse i get the REG_BINARY output.

Anyone has any idea how to convert it to the "normal" win10-key?

Edit: Readable = The normal windows Product-key (normally found on the sticker) for example: 9JNB6-GWD4G-JQHC7-*****-*****

Edit 2: VBS script that works.

    Const KeyOffset = 52
    Dim isWin10, Maps, i, j, Current, KeyOutput, Last, keypart1, insert
    'Check if OS is Windows 10
    isWin10 = (Key(66) \ 6) And 1
    Key(66) = (Key(66) And &HF7) Or ((isWin10 And 2) * 4)
    i = 24
    Maps = "BCDFGHJKMPQRTVWXY2346789"
    Do
           Current= 0
        j = 14
        Do
           Current = Current* 256
           Current = Key(j + KeyOffset) + Current
           Key(j + KeyOffset) = (Current \ 24)
           Current=Current Mod 24
            j = j -1
        Loop While j >= 0
        i = i -1
        KeyOutput = Mid(Maps,Current+ 1, 1) & KeyOutput
        Last = Current
    Loop While i >= 0
    keypart1 = Mid(KeyOutput, 2, Last)
    insert = "N"
    KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
    If Last = 0 Then KeyOutput = insert & KeyOutput
    ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5)

Upvotes: 1

Views: 2325

Answers (1)

Theo
Theo

Reputation: 61253

Here's that function in PowerShell. Hope it does what you want:

function Get-ProductKey {
    [cmdletbinding()]
    Param (
        [parameter(ValueFromPipeLine = $true, ValueFromPipeLineByPropertyName = $true)]
        [Alias("IPAddress", "Server")]
        [string[]]$Computername = $env:COMPUTERNAME
    )
    Begin {   
        $map = "BCDFGHJKMPQRTVWXY2346789" 
    }
    Process {
        foreach ($Computer in $Computername) {
            if (!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
                Write-Warning "Computer $Computer is unreachable"
                continue
            }
            # try and determine if this is a 64 or 32 bit machine
            try {
                $OS = Get-WmiObject -ComputerName $Computer -Class Win32_OperatingSystem -ErrorAction Stop                
            } 
            catch {
                Write-Warning "Could not retrieve OS version from computer $Computer"
                continue
            }
            # try and read the registry
            try {
                $remoteReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)
                $valueName = if ([int]($OS.OSArchitecture -replace '\D') -eq 64) { 'DigitalProductId4' } else { 'DigitalProductId' }
                $value = $remoteReg.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue($valueName)[0x34..0x42]
                $productKey = for ($i = 24; $i -ge 0; $i--) { 
                    $k = 0 
                    for ($j = 14; $j -ge 0; $j--) { 
                        $k = ($k * 256) -bxor $value[$j] 
                        $value[$j] = [math]::Floor([double]($k/24)) 
                        $k = $k % 24 
                    }
                    # output one character to collect in the $productKey array
                    $map[$k]
                    # output a hyphen
                    if (($i % 5) -eq 0 -and $i -ne 0) { "-" } 
                }
                # reverse the array
                [array]::Reverse($productKey)
                # output the ProductKey as string
                $productKey -join ''
            } 
            catch {
                Write-Warning "Could not read registry from computer $Computer"
            }        
            finally {
                if ($remoteReg) { $remoteReg.Close() }
            }
        } 
    }
}


Get-ProductKey

Upvotes: 3

Related Questions