Q:

Error message while activating Windows with batch file

I've been trying to make a batchfile which among other things automatically activates Windows. 
But by running it I get an error message:
@echo off
wmic path softwarelicensingservice get OA3xOriginalProductKey > %filepath%\temp.txt
type %filepath%\temp.txt > %filepath%\Pkey.txt
del %filepath%\temp.txt
set "pkey="
for /F "skip=1 delims=" %%i in (Pkey.txt) do set "pkey=%%i"
slmgr.vbs /ipk %pkey%

I added the type because I was having an issue with the productkey not being readable and giving an invalid output. I've tried echoing the variable %pkey% and it gives me the correct output. But when I try inserting it as the productkey I get the following error that the product key is missing. Is it a problem in my code or is it impossible to activate Windows this way using a variable?

A:

As far as your code goes, I would suggest doing it like this:
@Echo Off
Set "pkey="
For /F "Tokens=2 Delims==" %%A In ('
    WMIC Path SoftwareLicensingService Get OA3xOriginalProductKey /Value
') Do Set "pkey=%%A"
If Not Defined pkey Exit /B
Rem slmgr.vbs /ipk %pkey%

Whether or not this is what you need however is a different matter. I would have thought that your product key is already installed, therefore no need to get it then add it back in. For that reason I've Remarked the last line.

