Home  Tutorials  Active Directory


How to Get the Number of Computers per Windows Operating System in an Active Directory Domain using Powershell (EN)

In an Active Directorydomain, the details about the running version of anoperating systemare stored in two computer accounts’attributes:
  • operatingsystem: This attribute stores the name of the Operating System (Example: Windows Server 2012 R2 Datacenter)
  • operatingsystemversion: This attribute stores the version of the Operating System (Example: 6.3 (9600))


Operating system

Version number

Windows 10

10.0

Windows 8.1

6.3*

Windows Server 2016

10.0

Windows Server 2012 R2

6.3*

Windows 8

6.2

Windows Server 2012

6.2

Windows 7

6.1

Windows Server 2008 R2

6.1

Windows Server 2008

6.0

Windows Vista

6.0

Windows Server 2003 R2

5.2

Windows Server 2003

5.2

Windows XP 64-Bit Edition

5.2

Windows XP

5.1

Windows 2000

5.0


Operating System Version:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx


If you need to get the number of computers per Windows Operating System in yourActive Directorydomain, you can achieve that by doingLDAPqueries against your Active Directory.

The following Powershell script can be used to achieve this need:

------------------------------------------------------------------------------------------------------------------------------------
cls

$tableOSName = "OperatingSystems"

$tableOS = New-Object system.Data.DataTable “$tableOSName”

$colOS = New-Object system.Data.DataColumn OperatingSystem,([string])

$colOSversion = New-Object system.Data.DataColumn OperatingSystemVersion,([string])

$colOSType = New-Object system.Data.DataColumn OperatingSystemType,([string])

$tableOS.columns.add($colOS)

$tableOS.columns.add($colOSversion)

$tableOS.columns.add($colOSType)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 8.1"

$rowtableOS.OperatingSystemVersion = "6.3"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 8"

$rowtableOS.OperatingSystemVersion = "6.2"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 7"

$rowtableOS.OperatingSystemVersion = "6.1"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Vista"

$rowtableOS.OperatingSystemType = "WorkStation"

$rowtableOS.OperatingSystemVersion = "6.0"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows XP 64-Bit Edition"

$rowtableOS.OperatingSystemVersion = "5.2"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows XP"

$rowtableOS.OperatingSystemVersion = "5.1"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 2000 Professional"

$rowtableOS.OperatingSystemVersion = "5.0"

$rowtableOS.OperatingSystemType = "WorkStation"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2016"

$rowtableOS.OperatingSystemVersion = "10.0"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2012 R2"

$rowtableOS.OperatingSystemVersion = "6.3"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2012"

$rowtableOS.OperatingSystemVersion = "6.2"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2008 R2"

$rowtableOS.OperatingSystemVersion = "6.1"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server® 2008"

$rowtableOS.OperatingSystemVersion = "6.0"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows Server 2003"

$rowtableOS.OperatingSystemVersion = "5.2"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 2000 Server"

$rowtableOS.OperatingSystemVersion = "5.0"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 2000 Advanced Server"

$rowtableOS.OperatingSystemVersion = "5.0"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

$rowtableOS = $tableOS.NewRow()

$rowtableOS.OperatingSystem = "Windows 2000 Datacenter Server"

$rowtableOS.OperatingSystemVersion = "5.0"

$rowtableOS.OperatingSystemType = "Server"

$tableOS.Rows.Add($rowtableOS)

write-host "WorkStation Operating Systems : " -foregroundcolor "Green"

$WorkStationCount = 0

foreach ($object in ($tableOS | where {$_.OperatingSystemType -eq 'WorkStation'}))

{

$LDAPFilter = "(&(operatingsystem=" + $object.OperatingSystem + "*)(operatingsystemversion=" + $object.OperatingSystemVersion + "*))"

$OSCount = (Get-ADComputer -LDAPFilter $LDAPFilter).Count

if ($OSCount -ne $null)

{

"" + $object.OperatingSystem  + ": " + $OSCount + ""

}

else

{

"" + $object.OperatingSystem  + ": 0"

$OSCount = 0

}

$WorkStationCount += $OSCount

}

$WorkStationTotalNumber = "Total Number : " + $WorkStationCount + ""

write-host $WorkStationTotalNumber -foregroundcolor "Yellow"

write-host ""

write-host "Server Operating Systems : " -foregroundcolor "Green"

$ServerCount = 0

foreach ($object in ($tableOS | where {$_.OperatingSystemType -eq 'Server'}))

{

$LDAPFilter = "(&(operatingsystem=" + $object.OperatingSystem + "*)(operatingsystemversion=" + $object.OperatingSystemVersion + "*))"

$OSCount = (Get-ADComputer -LDAPFilter $LDAPFilter).Count

if ($OSCount -ne $null)

{

"" + $object.OperatingSystem  + ": " + $OSCount + ""

}

else

{

"" + $object.OperatingSystem  + ": 0"

$OSCount = 0

}

$ServerCount += $OSCount

}

$ServerTotalNumber = "Total Number : " + $ServerCount + ""

write-host $ServerTotalNumber -foregroundcolor "Yellow"

write-host ""

$LDAPFilter = "(&(operatingsystem=*)"

foreach ($object in $tableOS)

{

$LDAPFilter += "(!(&(operatingsystem=" + $object.OperatingSystem + "*)(operatingsystemversion=" + $object.OperatingSystemVersion + "*)))"

}

$LDAPFilter += ")"

$OthersCount = (Get-ADComputer -LDAPFilter $LDAPFilter).Count

$OthersTotalNumber = "Total Number : " + $OthersCount + ""

write-host "Other Operating Systems : " -foregroundcolor "Green"

write-host $OthersTotalNumber -foregroundcolor "Yellow"

------------------------------------------------------------------------------------------------------------------------------------

When you run this script, you will have the number of computers per Windows Operating System displayed:



You will find also the number of computers for “Other Operating Systems”.

This mainly represents:
  • Non-Windows computers
  • Windows Computers that have inconsistencies in their operatingsystem and operatingsystemversion attributes
  • Windows computers that are running operating systems lower than Windows 2000
How is the script working?

The script is using a table that has the following columns:
  • OperatingSystem
  • OperatingSystemVersion
  • OperatingSystemType (Server or WorkStation)


For each of the rows, the script will create the following LDAP filter:


$LDAPFilter
="(&(operatingsystem="+$object.OperatingSystem+"*)(operatingsystemversion="+$object.OperatingSystemVersion+"*))"


Where $object.OperatingSystem is a variable which contains the Operating System value in the row and $object.OperatingSystemVersion is a variable which contains the Operating System Version value in the row. The LDAP filter allows getting computer accounts that have:
  • operatingsystem attribute value starting with $object.OperatingSystem value
  • AND operatingsystemversion attribute value starting with $object.OperatingSystemVersion value

By using Get-ADComputer cmdlet with this LDAP filter, the script is able to get the list of computers per Operating System and count their number.

As for “Other Operating Systems”, the script is using the following LDAP filter:
  • Operatingsystem attribute has a value
  • AND the computer account do not belong to any of the previously mentioned LDAP filters
-----------------------------------------------------------------------------------------------------------------------------------

Other Languages

This article is available in other languages.