1. function Get-WindowsKey {
  2. ## function to retrieve the Windows Product Key from any PC
  3. ## by Jakob Bindslet ( This e-mail address is being protected from spambots, you need JavaScript enabled to view it )
  4. param ($targets = ".")
  5. $hklm = 2147483650
  6. $regPath = "Software\Microsoft\Windows NT\CurrentVersion"
  7. $regValue = "DigitalProductId4"
  8. Foreach ($target in $targets) {
  9. $productKey = $null
  10. $win32os = $null
  11. $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
  12. $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
  13. $binArray = ($data.uValue)[52..66]
  14. $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"
  15. ## decrypt base24 encoded binary data
  16. For ($i = 24; $i -ge 0; $i--) {
  17. $k = 0
  18. For ($j = 14; $j -ge 0; $j--) {
  19. $k = $k * 256 -bxor $binArray[$j]
  20. $binArray[$j] = [math]::truncate($k / 24)
  21. $k = $k % 24
  22. }
  23. $productKey = $charsArray[$k] + $productKey
  24. If (($i % 5 -eq 0) -and ($i -ne 0)) {
  25. $productKey = "-" + $productKey
  26. }
  27. }
  28. $win32os = Get-WmiObject Win32_OperatingSystem -computer $target
  29. $obj = New-Object Object
  30. $obj | Add-Member Noteproperty Computer -value $target
  31. $obj | Add-Member Noteproperty Caption -value $win32os.Caption
  32. $obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
  33. $obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
  34. $obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
  35. $obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
  36. $obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
  37. $obj | Add-Member Noteproperty ProductKey -value $productkey
  38. $obj
  39. }
  40. }
  41.  
  42. Get-WindowsKey "SERVERNAME"
  43.  
  44.