I was recently asked if I knew of a tool capable of retrieving the Product Key from an installed instance of Windows Vista. While there are several such tools available (use google), my solution - naturally - was to write a PowerShell script that can do just that.
The script below is based on information from "The Custodian" on Everything2:
http://everything2.com/node/1058074I've tested the function against Windows XP, Vista x64 and Windows 7 x64, as well as Windows Server 2003 and Windows Home Server. As long at the the user executing the script has sufficient privileges, and no firewall is blocking remote access to the registry/WMI the script appears to work fine.
function Get-WindowsKey { ## function to retrieve the Windows Product Key from any PC ## by Jakob Bindslet (jakob@bindslet.dk) param ($targets = ".") $hklm = 2147483650 $regPath = "Software\Microsoft\Windows NT\CurrentVersion" $regValue = "DigitalProductId" Foreach ($target in $targets) { $productKey = $null $win32os = $null $wmi = [WMIClass]"\\$target\root\default:stdRegProv" $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue) $binArray = ($data.uValue)[52..66] $charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9" ## decrypt base24 encoded binary data For ($i = 24; $i -ge 0; $i--) { $k = 0 For ($j = 14; $j -ge 0; $j--) { $k = $k * 256 -bxor $binArray[$j] $binArray[$j] = [math]::truncate($k / 24) $k = $k % 24 } $productKey = $charsArray[$k] + $productKey If (($i % 5 -eq 0) -and ($i -ne 0)) { $productKey = "-" + $productKey } } $win32os = Get-WmiObject Win32_OperatingSystem -computer $target $obj = New-Object Object $obj | Add-Member Noteproperty Computer -value $target $obj | Add-Member Noteproperty Caption -value $win32os.Caption $obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion $obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture $obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber $obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser $obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber $obj | Add-Member Noteproperty ProductKey -value $productkey $obj }}Use the function to retrieve the Product Key from the local PC:
Get-WindowsKeyOr to retrieve the Product Key from one or more PCs (locally or remotely):
Get-WindowsKey "pc1", "pc2", "server999", "server777"Remeber that the output from the function is a standard PowerShell object, so you can pipe into sort-object, format-table or mayby ConvertTo-HTML ...