Wmic to Get windows product key

Started by Abo-Zead, October 06, 2023, 08:38:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Abo-Zead

Hi All,

I hope this message finds you in good spirits.

Recently, I've been looking into a way to remotely fetch Windows product keys for multiple workstations using batch file. Through my research, I came across the WMIC command, which seems good. and need to extract these keys from a list of computer names in a text file and save them to an output text file, all in one go, but unfortunately it didn't work
here is the code that i used :


@echo off
setlocal EnableDelayedExpansion
set "inputFile=input.txt"
set "outputFile=output_WPK.txt"

for /f "delims=" %%A in (%inputFile%) do (
    set "computerName=%%A"
    echo Product Key for !computerName!: >> %outputFile%
    for /f "tokens=2 delims==" %%I in ('wmic /node:!computerName! path softwarelicensingservice get OA3xOriginalProductKey /value') do (
        echo %%I >> %outputFile%
    )
    echo. >> %outputFile%
)

echo Product Keys extracted and saved to %outputFile%
pause


And here the output data below


Product Key for C3I-PC1:
ECHO is off.
Product Key for C3I-PC2:
ECHO is off.
Product Key for C3I-PC3:
ECHO is off.


And i want to exetract the Computer name and product key Only under each other like that

[ ComputerName : Windows Product Key]
[ ComputerName : Windows Product Key]


Your expertise in this area would be immensely helpful. If you have any tips, best practices, or perhaps even a sample script, I'd be extremely grateful.
Thank you so much for your efforts

Warm regards,


Abo-Zead

Is there anyone here who can help me to make this code more effective

Geek-9pm

Your can buy something that might help you.
If your could just do it with code , Microsoft would stop it.
Search for a product key on Google. You might f ind one under$40.
Or talk to Microsoft support. In rare cases they will let you have it free.  :o
Look e for:
...how to get windows 10 for free

Squashman

1) Do not use variable names that are already system variables. computername is a system variable.
2) You do not need to assign %%A to a variable.  Just use %%A.

Other than that your code seems fine.  Works on my computer.
First thing I would do to troubleshoot is run this line of code with one computer to make sure you are getting output.
wmic /node:{Computer1} path softwarelicensingservice get OA3xOriginalProductKey /value