PowerShell Genie

Build a server health report object

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

Return free disk space, stopped-service count and an actual uptime duration in one structured PowerShell object.

Example reviewed 1 October 2026 · Illustrative output · Not a live system check

The script

$os = Get-CimInstance Win32_OperatingSystem -ErrorAction Stop
$disk = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" -ErrorAction Stop
if (-not $disk) { throw 'The C: disk was not found.' }
$stopped = @(Get-Service -ErrorAction Stop | Where-Object Status -eq 'Stopped')
[pscustomobject]@{
    Computer = $env:COMPUTERNAME
    FreeGB = [math]::Round($disk.FreeSpace / 1GB, 1)
    StoppedServices = $stopped.Count
    UptimeDays = [math]::Round(((Get-Date) - $os.LastBootUpTime).TotalDays, 1)
}

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

    Fictional reference snapshot · 03 October 2026, 09:00 UTC. This is an example result, not a check of your systems.

    Computer  FreeGB  StoppedServices  UptimeDays
    --------  ------  ---------------  ----------
    LAB-SRV1  48.2    2                3.5
    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.

    $os = Get-CimInstance Win32_OperatingSystem -ErrorAction Stop $disk = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" -ErrorAction Stop if (-not $disk) { throw 'The C: disk was not found.' } $stopped = @(Get-Service -ErrorAction Stop | Where-Object Status -eq 'Stopped') [pscustomobject]@{ Computer = $env:COMPUTERNAME FreeGB = [math]::Round($disk.FreeSpace / 1GB, 1) StoppedServices = $stopped.Count UptimeDays = [math]::Round(((Get-Date) - $os.LastBootUpTime).TotalDays, 1) }
    1. Stage 1$osPositional values or no named parameters

      Illustrative output

      This example shows the expected shape for fictional data. Your values and formatting will differ.

      Computer  FreeGB  StoppedServices  UptimeDays
      SRV01     128.5   4                3.1

      How it works

      UptimeDays is the difference between the current time and LastBootUpTime, expressed in days. Returning LastBootUpTime directly would be a timestamp, not uptime. FreeGB uses PowerShell’s binary 1GB multiplier. The object remains suitable for filtering and CSV export.

      Before you run it

      This example targets Windows with local CIM and service access. Change the disk filter deliberately if C: is not the volume you need. Any failed measurement stops this example rather than silently producing a misleading report.

      Check the result

      Compare disk values with the selected volume and inspect the stopped services individually. A stopped manual or trigger-start service can be normal. Use application-specific checks before calling a server healthy.

      If nothing is returned

      This script should return one object or raise an error. Treat missing output as a reason to inspect errors, not as a healthy result.

      Practise the underlying skill

      Try a related exercise →

      Read the primary Microsoft documentation

      Genie guideBuild a server health report object

      Return free disk space, stopped-service count and an actual uptime duration in one structured PowerShell object.