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').OA3xOriginalProductKeyXXXXX-XXXXX-XXXXX-XXXXX-XXXXXCommand Promptwmic path softwarelicensingservice get OA3xOriginalProductKeyXXXXX-XXXXX-XXXXX-XXXXX-XXXXXPowerShell((Get-ItemPropertyValue -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId) | ForEach-Object ToString X2) -join ','XXXXX-XXXXX-XXXXX-XXXXX-XXXXXRegistry Editor (regedit)HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion in the location barXXXXX-XXXXX-XXXXX-XXXXX-XXXXX