0

I'm trying to make a script for checking license in my computer. I want to get the output like this:

Name: Windows(R), Professional edition
 > PartialProductKey: 3V66T
 > LicenseStatus: 1
Name: Office 16, Office16ProPlusVL_KMS_Client edition
 > PartialProductKey: WFG99
 > LicenseStatus: 1

Here is the code to check for Name:

for /f "tokens=2 delims==" %%b in ('"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get Name /value"') do (echo Name: %%b)

For PartialProductKey:

for /f "tokens=2 delims==" %%b in ('"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get PartialProductKey /value"') do (echo PartialProductKey: %%b)

and for LicenseStatus:

for /f "tokens=2 delims==" %%b in ('"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get LicenseStatus /value"') do (echo LicenseStatus: %%b)

But how can I show the PartialProductKey, LicenseStatus below each Name? Thanks!

dphuc23
  • 63
  • 5

2 Answers2

1

wmic does strange things with its output (it's actually the interaction between WMIC and FOR /F, adding an additional \r before \r\n. The behavior is discussed widespread across SO, like here). That's why findstr /V /R "^$" is needed:

@echo off
setlocal EnableDelayedExpansion
FOR /F tokens^=*^ delims^=^ eol^= %%L in (
'"wmic path SoftwareLicensingProduct where (PartialProductKey is not null) get Name,PartialProductKey,LicenseStatus /format:list|findstr /V /R "^^$""'
) do (
    set "%%L"
    echo Name: !name!
    echo  ^> PartialProductKey: !PartialProductKey!
    echo  ^> LicenseStatus: !LicenseStatus!
)
ScriptKidd
  • 803
  • 1
  • 5
  • 19
1

You could use the standard built-in VBScripts to get the information you require:

@%__AppDir__%cscript.exe /NoLogo %__AppDir__%slmgr.vbs -DLi
@For %%G In (14 15 16) Do @If Exist "%ProgramFiles%\Microsoft Office\Office%%G\ospp.vbs" %__AppDir__%cscript.exe /NoLogo "%ProgramFiles%\Microsoft Office\Office%%G\ospp.vbs" /DStatus | %__AppDir__%findstr.exe /I "^License\>"
@Pause
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you so much. However, I prefer to use the WMIC command over slmgr.vbs or ospp.vbs to avoid the case when those files are deleted by the user, etc. – dphuc23 May 10 '20 at 11:32
  • And my problem is solved. Thank you for your attention :D – dphuc23 May 10 '20 at 11:33
  • @dphuc23, if you have users with sufficient privileges to remove files from the protected locations, _(`System32` and `Program Files`)_, you really should remove their privileges, or better still, the users! – Compo May 10 '20 at 11:38