PowerShell Genie

Inventory Windows build information

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

One Get-CimInstance call against Win32_OperatingSystem returns a single instance per machine.

The script

1PS> Get-CimInstance Win32_OperatingSystem | Select-Object CSName,Caption,Version,BuildNumber,OSArchitecture,LastBootUpTime

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 CSName Caption Version BuildNumber OSArchitecture LastBootUpTime
    2 ------ ------- ------- ----------- -------------- --------------
    3 SRV01 Microsoft Windows Server 2022 Standard 10.0.20348 20348 64-bit 9/12/2026 6:15:00 AM
    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-CimInstance Win32_OperatingSystem | Select-Object CSName,Caption,Version,BuildNumber,OSArchitecture,LastBootUpTime
    1. Stage 1Get-CimInstancePositional values or no named parameters
    2. Stage 2Select-ObjectPositional values or no named parameters

      How it works

      One Get-CimInstance call against Win32_OperatingSystem returns a single instance per machine. Select-Object narrows it to CSName for the host name, Caption for the edition string, Version and BuildNumber for the servicing level, plus OSArchitecture and LastBootUpTime. Because this goes through CIM rather than the retired WMI cmdlets, LastBootUpTime arrives as a real DateTime with no DMTF string to parse.

      Adapting it

      Add -ComputerName with an array of names to fan the same query across a fleet, and append Export-Csv when the output is destined for a compliance workbook.

      If the result is unexpected

      The build number here disagrees with what Winver reports, or a comparison against an expected edition string fails even though the text on screen looks identical.

      Version and BuildNumber report the base build without the update revision, so a fully patched machine looks the same as an unpatched one at the same build. Caption carries trailing whitespace on several server editions, which quietly breaks equality tests. Neither field is wrong; both mislead a comparison built on them.

      1. Read UBR from HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion to recover the revision the base build omits.
      2. Trim Caption before comparing, or match with -like and a wildcard rather than -eq.
      3. Cross-check with Get-ComputerInfo, which surfaces the fuller version detail in one object.
      4. Confirm LastBootUpTime arrives as a DateTime, since older WMI code paths return a DMTF-formatted string instead.
      5. Compare against Get-HotFix output if the underlying question is really about patch state.

      Validate the result

      BuildNumber combined with UBR matches the version string Winver shows exactly, and the edition comparison now returns true for a machine you know matches.

      Keep the check reliable

      Compare on build and revision numbers rather than on edition text, and keep the trim inside any function that hands Caption back to a caller.

      Primary documentation

      Get-CimInstance reference

      Continue
      Genie guideInventory Windows build information

      One Get-CimInstance call against Win32_OperatingSystem returns a single instance per machine.