Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Windows License Type

I am trying to pull the Windows license type, I can do it with the following command but it takes forever, is there a quicker PowerShell command for accomplishing this?

See code below for what I've tried.

(Get-CimInstance SoftwareLicensingProduct |
  Where-Object -FilterScript { 
    ($_.Description -like "W*" -and 
    $_.licensestatus -eq 1 ) 
  } |
    Select-Object -First 1 -ExpandProperty Description
) -replace '.*(VOLUME_MAK|OEM_SLP|RETAIL|OEM_COA_NSLP|OEM_COA_SLP).*', '$1'

I get the expected result but it is slow.

like image 475
onefiscus Avatar asked Dec 30 '25 14:12

onefiscus


2 Answers

Get-CimInstance has a -Filter parameter that accepts a WQL query filter:

$SLP = Get-CimInstance SoftwareLicensingProduct -Filter 'Description LIKE "Windows%" AND LicenseStatus = 1'
$SLP.Description -replace '.*(VOLUME_MAK|OEM_SLP|RETAIL|OEM_COA_NSLP|OEM_COA_SLP).*', '$1'

This will likely be faster than having Get-CimInstance return all the instances and then filter on them.

You may want to add VOLUME_KMSCLIENT to the regex pattern :-)

like image 190
Mathias R. Jessen Avatar answered Jan 01 '26 06:01

Mathias R. Jessen


Querying at the level of Cim instance is faster than that in PowerShell.

In your code, you are getting the whole CimInstance related to SoftwareLicensingProduct and then filtering them all (using where-object) in PowerShell, whereas you can use -Filter parameter and filter at the level of CimInstance, which is faster.

Try this:

(Get-CimInstance softwarelicensingproduct -filter 'Description LIKE "W%" AND
LicenseStatus = 1' |
    Select-Object -first 1 -ExpandProperty Description
) -replace '.*(VOLUME_MAK|OEM_SLP|RETAIL|OEM_COA_NSLP|OEM_COA_SLP).*', '$1'

You can play around with the filter parameter and get narrower results. Look here for further help on WQL and WMI

like image 23
Shadowfax Avatar answered Jan 01 '26 06:01

Shadowfax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!