For (possibly) all versions of Windows. Tested on Windows 10 and 11.
To show information about the product key (including the last 5 characters) use command slmgr /dlv
in PowerShell or Command Prompt.
NOTE: Always double check using the above command that the extracted product key is the one used by the system. If, for example, the product key has been changed due upgrading from Windows Home to Windows Pro the product key found might be the old one. If that is the case, continue with other methods.
PowerShell
(Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Command Prompt
wmic path softwarelicensingservice get OA3xOriginalProductKey
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
PowerShell
((Get-ItemPropertyValue -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId) | ForEach-Object ToString X2) -join ','
XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
Registry Editor
(regedit)HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
in the location barXXXXX-XXXXX-XXXXX-XXXXX-XXXXX