2

Am trying to write the output of this command into a variable, but can't quite get the syntax to work...

My WMIC query is:

wmic PATH SoftwareLicensingProduct WHERE "ProductKeyID like '%-%' AND Description like '%Windows%'" get LicenseStatus

Can anyone help?

2
  • What is the output from running your command?
    – DavidPostill
    Sep 17, 2015 at 11:26
  • The output is: LicenseStatus 1 I want to capture the 1 in a variable
    – WeeYin
    Sep 17, 2015 at 12:34

2 Answers 2

2

The output is: LicenseStatus 1, I want to capture the 1 in a variable

Use the following batch file:

@echo off
for /f "usebackq tokens=2" %%i in (`wmic PATH SoftwareLicensingProduct WHERE "ProductKeyID like '%-%' AND Description like '%Windows%'" get LicenseStatus`) do set _variable=%%i
@echo %_variable%

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • set - Display, set, or remove CMD environment variables. Changes made with SET will remain only for the duration of the current CMD session.
2
  • Thanks for the your suggestion, when I run this as a batch file I receive "No Instance Available". When I run the wmic query separately I get the output I expect.
    – WeeYin
    Sep 23, 2015 at 8:29
  • @WeeYin I've just noticed an error in my answer (I had %%1 instead of %%i which has been corrected). Please try again.
    – DavidPostill
    Oct 1, 2015 at 15:44
-1

Just use:

@echo off
for /f "usebackq tokens=2" %%i in (`wmic PATH SoftwareLicensingProduct WHERE "ProductKeyID like '%%-%%' AND Description like '%%Windows%%'" get LicenseStatus`) do set _variable=%%i
@echo %_variable%

It's basically the code from the first answer, but with doubled per cent signs.

BB

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .