# Script to check OS license details on Windows devices

OS licenses are agreements that allow users to legally use and access the software. It is important for admins to ensure that these licenses comply with the terms of the organization, this includes ensuring that the correct number of licenses are being used, and that the licenses are valid and not compromised. However, manually checking the license detail on every device is tiresome. Hence, you can [deploy](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) scripts from the Hexnode portal to fetch license details on every Windows device remotely.

 Disclaimer:The sample scripts provided below are adapted from third-party open-source sites.

 

PowerShell script to check license details
------------------------------------------

The following script will fetch license details like License name, Description, Application ID, Product Key Channel, License URL, Validation URL, Partial Product Key, Product Key ID, License Status, and Product Key.

$lic = Get-WmiObject -Class SoftwareLicensingProduct | Where-Object {$\_.PartialProductKey -ne $null} $id = (Get-WmiObject -query 'select \* from SoftwareLicensingService').OA3xOriginalProductKey echo "License Name: $($lic.Name)" echo "Descriptions: $($lic.Description)" echo "Application ID: $($lic.ApplicationId)" echo "Product Key Channel: $($lic.ProductKeyChannel)" echo "Use License URL: $($lic.UseLicenseURL)" echo "Validation URL: $($lic.ValidationURL)" echo "PartialProductKey: $($lic.PartialProductKey)" echo "ProductKey ID: $($lic.ProductKeyID)" echo "License Status: $($lic.LicenseStatus)" echo "Product Key: $id" 

   1

2

3

4

5

6

7

8

9

10

11

12

  $lic = Get-WmiObject -Class SoftwareLicensingProduct | Where-Object {$\_.PartialProductKey -ne $null}

$id = (Get-WmiObject -query 'select \* from SoftwareLicensingService').OA3xOriginalProductKey

echo "License Name: $($lic.Name)"

echo "Descriptions: $($lic.Description)"

echo "Application ID: $($lic.ApplicationId)"

echo "Product Key Channel: $($lic.ProductKeyChannel)"

echo "Use License URL: $($lic.UseLicenseURL)"

echo "Validation URL: $($lic.ValidationURL)"

echo "PartialProductKey: $($lic.PartialProductKey)"

echo "ProductKey ID: $($lic.ProductKeyID)"

echo "License Status: $($lic.LicenseStatus)"

echo "Product Key: $id" 

   

 

  

The following values specify the status of the license:

ValueDescription0Unlicensed1Licensed2Out-Of-Box Grace 3Out-Of-Tolerance Grace 4NonGenuineGrace 5Notification 6ExtendedGrace Alternatively, you can also use the following script to find the license details like the OS version, Product ID, and Product Key of devices running on a custom Windows OS.

try { #vbScript to get product key $vbScript = @" Option Explicit Dim objshell,path,DigitalID, Result Set objshell = CreateObject("WScript.Shell") 'Set registry key path Path = "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\" 'Registry key value DigitalID = objshell.RegRead(Path & "DigitalProductId") Dim ProductName,ProductID,ProductKey,ProductData 'Get ProductName, ProductID, ProductKey ProductName = "Product Name: " & objshell.RegRead(Path & "ProductName") ProductID = "Product ID: " & objshell.RegRead(Path & "ProductID") ProductKey = "Product Key: " & ConvertToKey(DigitalID) ProductData = ProductName & vbNewLine & ProductID & vbNewLine & ProductKey WScript.Echo ProductData 'Convert binary to chars Function ConvertToKey(Key) Const KeyOffset = 52 Dim isWin8, Maps, i, j, Current, KeyOutput, Last, keypart1, insert 'Check if OS is Windows 8 isWin8 = (Key(66) \\ 6) And 1 Key(66) = (Key(66) And &HF7) Or ((isWin8 And 2) \* 4) i = 24 Maps = "BCDFGHJKMPQRTVWXY2346789" Do Current= 0 j = 14 Do Current = Current\* 256 Current = Key(j + KeyOffset) + Current Key(j + KeyOffset) = (Current \\ 24) Current=Current Mod 24 j = j -1 Loop While j >= 0 i = i -1 KeyOutput = Mid(Maps,Current+ 1, 1) & KeyOutput Last = Current Loop While i >= 0 If (isWin8 = 1) Then keypart1 = Mid(KeyOutput, 2, Last) insert = "N" KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0) If Last = 0 Then KeyOutput = insert & KeyOutput End If ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5) End Function "@ $vbScript | Out-File "C:\\Hexnode\\licenseScript.vbs" start-process cscript.exe -argumentlist "C:\\Hexnode\\licenseScript.vbs" -redirectstandardoutput "C:\\Hexnode\\license.txt" -wait $productkey = get-content "C:\\Hexnode\\license.txt" $productkey Remove-Item "C:\\Hexnode\\license.txt" Remove-Item "C:\\Hexnode\\licenseScript.vbs" } catch { Write-Host $\_.Exception.Message } 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

  try

{

 \#vbScript to get product key

 $vbScript = @"

Option Explicit 

Dim objshell,path,DigitalID, Result 

Set objshell = CreateObject("WScript.Shell") 

'Set registry key path 

Path = "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\" 

'Registry key value 

DigitalID = objshell.RegRead(Path & "DigitalProductId") 

Dim ProductName,ProductID,ProductKey,ProductData 

'Get ProductName, ProductID, ProductKey 

ProductName = "Product Name: " & objshell.RegRead(Path & "ProductName") 

ProductID = "Product ID: " & objshell.RegRead(Path & "ProductID") 

ProductKey = "Product Key: " & ConvertToKey(DigitalID) 

ProductData = ProductName & vbNewLine & ProductID & vbNewLine & ProductKey 

WScript.Echo ProductData

'Convert binary to chars 

Function ConvertToKey(Key) 

 Const KeyOffset = 52 

 Dim isWin8, Maps, i, j, Current, KeyOutput, Last, keypart1, insert 

 'Check if OS is Windows 8 

 isWin8 = (Key(66) \\ 6) And 1 

 Key(66) = (Key(66) And &HF7) Or ((isWin8 And 2) \* 4) 

 i = 24 

 Maps = "BCDFGHJKMPQRTVWXY2346789" 

 Do 

 Current= 0 

 j = 14 

 Do 

 Current = Current\* 256 

 Current = Key(j + KeyOffset) + Current 

 Key(j + KeyOffset) = (Current \\ 24) 

 Current=Current Mod 24 

 j = j -1 

 Loop While j >= 0 

 i = i -1 

 KeyOutput = Mid(Maps,Current+ 1, 1) & KeyOutput 

 Last = Current 

 Loop While i >= 0 

 If (isWin8 = 1) Then 

 keypart1 = Mid(KeyOutput, 2, Last) 

 insert = "N" 

 KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0) 

 If Last = 0 Then KeyOutput = insert & KeyOutput 

 End If 

 ConvertToKey = Mid(KeyOutput, 1, 5) & "-" & Mid(KeyOutput, 6, 5) & "-" & Mid(KeyOutput, 11, 5) & "-" & Mid(KeyOutput, 16, 5) & "-" & Mid(KeyOutput, 21, 5) 

End Function

"@

$vbScript | Out-File "C:\\Hexnode\\licenseScript.vbs"

start-process cscript.exe -argumentlist "C:\\Hexnode\\licenseScript.vbs" -redirectstandardoutput "C:\\Hexnode\\license.txt" -wait

$productkey = get-content "C:\\Hexnode\\license.txt"

$productkey

Remove-Item "C:\\Hexnode\\license.txt"

Remove-Item "C:\\Hexnode\\licenseScript.vbs"

}

catch

{

 Write-Host $\_.Exception.Message

} 

   

 

  

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.