How to Get the Windows Product Key in a Batch Script
Whether you are preparing for a clean reinstallation of Windows, performing a system audit, or simply want to keep a record of your license, retrieving your Windows product key is a common administrative task. On most modern computers (Windows 8, 10, and 11), the product key is no longer printed on a sticker on the machine. Instead, for OEM (Original Equipment Manufacturer) licenses, it is embedded directly into the computer's BIOS or UEFI firmware.
This guide will teach you how to retrieve this embedded product key using a simple, built-in Windows utility. You will learn the recommended WMIC command to query the firmware directly and the modern PowerShell equivalent, allowing you to get the key from any batch script.
The Challenge: Where is the Product Key Stored?
On older versions of Windows (like 7 and XP), the key was often stored in the registry. On modern PCs that came with Windows pre-installed, the key is stored in a specific table in the BIOS/UEFI called the OA3 (OEM Activation 3.0) table. This allows Windows to automatically activate itself during installation without the user needing to type anything. Our script needs to query this specific location.
The Core Method (Recommended): Using WMIC
The WMIC (Windows Management Instrumentation Command-line) utility can directly query the SoftwareLicensingService, which has access to the firmware-embedded key.
Command: WMIC Path SoftwareLicensingService GET OA3xOriginalProductKey
Path SoftwareLicensingService: TellsWMICto query this specific WMI class.GET OA3xOriginalProductKey: Requests the specific property that holds the OEM product key.
The Modern Alternative: Using PowerShell
PowerShell provides a more modern syntax for accessing the same WMI object. This method is equally effective and often preferred in modern scripting environments.
Command: powershell -Command "(Get-WmiObject -query 'select * from SoftwareLicensingService').OA3xOriginalProductKey"
This one-liner performs the exact same query as the WMIC command and returns the same result.
The Unreliable Legacy Method (To Avoid): Querying the Registry
Many old online guides suggest querying a registry key like HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId.
This method is not reliable on modern systems.
- On systems that were upgraded to Windows 10/11, this key often holds a generic product key, not the actual key used for activation.
- It does not retrieve the key from the BIOS/UEFI.
For these reasons, you should avoid the registry query method and use WMIC or PowerShell instead.
The Script: Capturing the Product Key into a Variable
This script uses the recommended WMIC command and a FOR /F loop to capture the output and store it in a variable.
@ECHO OFF
SETLOCAL
SET "ProductKey="
ECHO --- Retrieving Windows Product Key ---
ECHO This will get the key embedded in your system's firmware (BIOS/UEFI).
ECHO.
REM The 'skip=1' ignores the header line "OA3xOriginalProductKey".
REM The second FOR loop is a trick to remove invisible trailing characters.
FOR /F "skip=1" %%A IN ('WMIC Path SoftwareLicensingService GET OA3xOriginalProductKey') DO (
FOR %%B IN (%%A) DO SET "ProductKey=%%B"
)
IF NOT DEFINED ProductKey (
ECHO [INFO] No product key was found in the firmware.
ECHO This is common on custom-built PCs or systems with volume licenses.
) ELSE (
ECHO The product key is: %ProductKey%
)
ENDLOCAL
PAUSE
Common Pitfalls and How to Solve Them
-
Administrator Rights: While this query can sometimes work as a standard user, it is most reliable when run as an Administrator. An elevated prompt ensures that
WMIChas the necessary permissions to query system-level services. -
OEM vs. Retail Keys: This method finds the original key that the manufacturer embedded in the firmware. If you later purchased a different version of Windows (e.g., upgrading from Home to Pro with a new retail key), this command will still show you the original OEM Home key, not the new Pro key you're using.
-
No Key Found: The command may return no key. This is normal and expected in several scenarios:
- On a custom-built PC where Windows was installed from a retail copy.
- On machines that are part of a corporate volume license (KMS or MAK).
- On older hardware that pre-dates the BIOS/UEFI key embedding practice.
Practical Example: A System Information Report
This script gathers the product key along with other key system information and saves it to a text file on the user's desktop.
@ECHO OFF
SETLOCAL
SET "ReportFile=%USERPROFILE%\Desktop\System_License_Info.txt"
SET "ProductKey="
ECHO --- Generating System License Report ---
ECHO Saving information to "%ReportFile%"...
REM --- Get the Product Key ---
FOR /F "skip=1" %%A IN ('WMIC Path SoftwareLicensingService GET OA3xOriginalProductKey') DO (
FOR %%B IN (%%A) DO SET "ProductKey=%%B"
)
IF NOT DEFINED ProductKey SET "ProductKey=Not found in firmware (Retail/Volume License?)"
REM --- Get the OS Name ---
FOR /F "tokens=2 delims==" %%C IN ('WMIC OS GET Caption /VALUE') DO SET "OS_Name=%%C"
FOR %%N IN ("%OS_Name%") DO SET "OS_Name=%%~N"
REM --- Write the report ---
(
ECHO System Information Report
ECHO Generated on: %DATE% %TIME%
ECHO =================================
ECHO Computer Name: %COMPUTERNAME%
ECHO OS: %OS_Name%
ECHO Product Key: %ProductKey%
) > "%ReportFile%"
ECHO.
ECHO [SUCCESS] Report created on your desktop.
ENDLOCAL
Conclusion
The WMIC and PowerShell commands provide a direct and reliable way to retrieve the OEM product key embedded in a modern computer's firmware.
- The core command is
WMIC Path SoftwareLicensingService GET OA3xOriginalProductKey. - This method is vastly superior to outdated registry-querying techniques.
- Remember that it finds the original OEM key, which may not be the one currently in use if you've upgraded with a retail license.
- The command must be run as an Administrator for the most reliable results.