Enhancing System Health Checks with PowerShell

Keeping your systems healthy and performance-optimized is crucial for ensuring that your IT environment runs smoothly. Regular system health checks help identify issues before they become critical problems. This PowerShell script automates health checks for Windows servers, providing a summary of vital system metrics including CPU usage, memory usage, disk space, and service status.
This script will:
1. Check CPU and memory usage.
2. Report available disk space on all drives.
3. Verify the status of essential services.
Running this script on a scheduled basis can help maintain your infrastructure’s health.

# Get CPU Usage
$cpuUsage = Get-Counter '\Processor(_Total)\% Processor Time'
$cpuLoad = $cpuUsage.Counters[0].CookedValue
# Get Memory Usage
$memInfo = Get-WmiObject Win32_OperatingSystem
$memAvailableGB = [math]::round(($memInfo.FreePhysicalMemory / 1MB), 2)
$memTotalGB = [math]::round(($memInfo.TotalVisibleMemorySize / 1MB), 2)
$memUsagePercent = [math]::round(($memTotalGB - $memAvailableGB) / $memTotalGB * 100, 2)
# Get Disk Space
$diskInfo = Get-PSDrive -PSProvider FileSystem
# Get Service Status (Example service - set your critical services here)
$criticalServices = @('wuauserv', 'bits', 'eventlog')
$serviceStatus = @{}
foreach ($service in $criticalServices) {
    $serviceStatus[$service] = (Get-Service -Name $service).Status
}
# Output Results
Write-Host "=== System Health Check Results ==="
Write-Host "CPU Load: $cpuLoad %"
Write-Host "Memory Usage: $memUsagePercent % (Available: $memAvailableGB GB, Total: $memTotalGB GB)"
Write-Host "Disk Space:"
foreach ($disk in $diskInfo) {
    Write-Host "$($disk.Name): $([math]::round($disk.Used / 1GB, 2)) GB used, $([math]::round($disk.Free / 1GB, 2)) GB free"
}
Write-Host "Service Status:"
foreach ($service in $criticalServices) {
    Write-Host "$service: $($serviceStatus[$service])"
}