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.
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 :-)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With