PowerShell Genie

Summarize computers by OS

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Query AD computer operating systems, group matching values, and sort by count to report how many machine accounts use each OS.

The script

1PS> Get-ADComputer -Filter * -Properties OperatingSystem | Group-Object OperatingSystem | Sort-Object Count -Descending | Select-Object Name,Count

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 Name Count
    2 ---- -----
    3 Windows 11 Enterprise 148
    4 Windows Server 2022 32
    5 Windows Server 2019 11
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    Get-ADComputer -Filter * -Properties OperatingSystem | Group-Object OperatingSystem | Sort-Object Count -Descending | Select-Object Name,Count
    1. Stage 1Get-ADComputer-Filter -Properties
    2. Stage 2Group-ObjectPositional values or no named parameters
    3. Stage 3Sort-Object-Descending
    4. Stage 4Select-ObjectPositional values or no named parameters

      How it works

      Get-ADComputer retrieves every machine account with the OperatingSystem attribute requested, Group-Object buckets them by that value, Sort-Object puts the largest population first, and the projection reports each OS string as Name with its Count.

      Adapting it

      Add -Filter "Enabled -eq $true" to the retrieval so disabled leftovers stop inflating totals that are about to end up in a project plan.

      Requirements

      ActiveDirectory module.

      If the result is unexpected

      The operating-system summary has unexpected blank or duplicate categories.

      Directory OS attributes may be stale, missing or inconsistent across builds and editions.

      1. Inspect representative computer records in each category before deciding how to normalize labels.

      Validate the result

      Category totals reconcile with the queried computer count, including an explicit unknown category.

      Keep the check reliable

      Retain original OS values alongside normalized reporting labels.

      Primary documentation

      PowerShell objects and pipelines documentation

      Continue
      Genie guideSummarize computers by OS

      Query AD computer operating systems, group matching values, and sort by count to report how many machine accounts use each OS.