← script library

Automating System Health Check with PowerShell

Monitoring & HealthJanuary 5, 2025

This PowerShell script is designed to automate the process of checking the health of a Windows system. It gathers various system metrics such as CPU usage, available memory, and disk space, and outputs the information in a readable format. This guide provides a detailed, step-by-step explanation of how the script works, making it easier for IT professionals and system administrators to maintain their systems effectively.

powershell
# Variables for system performance metrics
$cpuMetric = Get-Counter \Processor(_Total)\% Processor Time
$memMetric = Get-Counter \Memory\Available MBytes
$diskMetric = Get-Counter \LogicalDisk(_Total)\% Free Space

# Store formatted data in a custom PSObject
$healthReport = [pscustomobject]@{
    TimeChecked = Get-Date
    CPUUsage    = $cpuMetric.CounterSamples.CookedValue
    AvailableMemory = $memMetric.CounterSamples.CookedValue
    FreeDiskSpace = $diskMetric.CounterSamples.CookedValue
}

# Output health report to the console
Write-Host "System Health Report"
Write-Host "===================="
Write-Host "Time Checked: $($healthReport.TimeChecked)"
Write-Host "CPU Usage: $([math]::Round($healthReport.CPUUsage, 2))%"
Write-Host "Available Memory: $([math]::Round($healthReport.AvailableMemory, 2)) MB"
Write-Host "Free Disk Space: $([math]::Round($healthReport.FreeDiskSpace, 2))%"

# Export health report to a text file
$reportPath = "C:\SystemHealthReport.txt"
$healthReport | Out-File -FilePath $reportPath -Encoding UTF8
Write-Host "Health report exported to: $reportPath"

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.