This PowerShell script monitors system performance by checking CPU and memory usage, providing an overview of resource consumption and potential bottlenecks.
powershell
$cpuUsage = Get-WmiObject -Class Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average
$memInfo = Get-WmiObject -Class Win32_OperatingSystem
$totalMemory = $memInfo.TotalVisibleMemorySize
$freeMemory = $memInfo.FreePhysicalMemory
$usedMemory = $totalMemory - ($freeMemory * 1MB)
$memoryUsagePercentage = [math]::round(($usedMemory / $totalMemory) * 100, 2)
$performanceReport = @{
'CPU Usage (%)' = $cpuUsage
'Used Memory (MB)' = [math]::round($usedMemory / 1MB, 2)
'Free Memory (MB)' = [math]::round($freeMemory / 1MB, 2)
'Memory Usage (%)' = $memoryUsagePercentage
}
$performanceReport.GetEnumerator() | ForEach-Object {
Write-Host "$($_.Key): $($_.Value)"
}
$logFilePath = "C:\Path\To\Your\PerformanceLog.txt"
$dateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$dateTime - CPU Usage: $cpuUsage%, Memory Usage: $memoryUsagePercentage%"
Add-Content -Path $logFilePath -Value $logEntry
if ($cpuUsage -gt 80 -or $memoryUsagePercentage -gt 80) {
[System.Windows.MessageBox]::Show("ALERT: High System Resource Usage! Review performance metrics.", "Performance Notification", 0, [System.Windows.MessageBoxIcon]::Warning)
}
# This step typically involves using Task Scheduler within Windows, so no specific PowerShell code is provided here.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.
More in Monitoring & Health
Disk Space Cleanup and Report Script
2025-01-14User Account Status Reporting Script
2025-01-13Automated Disk Space Monitoring Script
2025-01-13Automating System Health Check with PowerShell
2025-01-05Automate System Performance Monitoring with PowerShell
2024-12-30Enhancing System Monitoring with PowerShell: Real-time Log Management
2024-12-29Ready when you are.
Try ServerEngine free for 7 days.