Wmic output into variable

batchwmic

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?

Best Answer

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.
Related Question