コマンドプロンプトでこのコマンドを入力
wmic path SoftwareLicensingService get OA3xOriginalProductKey
時々これで取得できない環境がある
他の方法を探すと PowerShell で遠回しに↑と同じ処理を実行してるものだったり
レジストリを参照してるけどプロダクトキーじゃなくてプロダクト ID の取得方法が書かれていたり
ID は桁数が違うからみればわかるはず
使える方法だとソフトのインストールが必要になったりであまりやりたくない方法
スクリプトを探すとこういうのがあったけど VBS
https://gist.github.com/craigtp/dda7d0fce891a087a962d29be960f1da
実行するだけなら VBS で困らない気はするけど気持ち的にイヤなのと読みづらいのでなにしてるかわかりづらいのがなんかイヤだったので PowerShell のスクリプトにした
$dpid = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").DigitalProductId
$is_win8 = [Math]::Truncate($dpid[66] / 6) -band 1
$dpid[66] = ($dpid[66] -band 0xF7) -bor (($is_win8 -band 2) * 4)
$keyoffset = 52
$maps = "BCDFGHJKMPQRTVWXY2346789"
$key = ""
$last = 0
for ($i = 24; $i -ge 0; $i--) {
$current = 0
for ($j = 14; $j -ge 0; $j--) {
$current = $current * 256
$index = $j + $keyoffset
$current = $dpid[$index] + $current
$dpid[$index] = [Math]::Truncate($current / 24)
$current = $current % 24
}
$key = $maps[$current] + $key
$last = $current
}
$key = $key.Substring(1).Insert($last, "N")
$key = ($key -split "(.{5})" | ? { $_ }) -join "-"
echo $key
wmic コマンドでキーを取得できる環境でこのスクリプトを動かして同じ結果になることを確認したのでたぶん大丈夫なはず
wmic コマンドで取得できない環境でこの方法で取得できるかは未確認