Get RAM usage

Posted:  June 24, 2015

Example script to get RAM usage and inform the admin if it is at normal, high or critical level.

$WARNLEVEL = 65
$CRITLEVEL = 80

$ramusage = gwmi -Class win32_operatingsystem | Select-Object @{Name = "MemoryUsage"; Expression = {"{0:N2}" -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize) }}

if ( ($ramusage.MemoryUsage) -lt $WARNLEVEL ) {
	Write-Host "[OK] MEMORY usage is normal: "($ramusage.MemoryUsage)"%"
	exit 0
}
elseif (  ($ramusage.MemoryUsage) -gt $CRITLEVEL ) {
	Write-Host "[PROBLEM] MEMORY usage is high: "($ramusage.MemoryUsage)"%"
	exit 2
}
else {
	Write-Host "[WARNING] MEMORY usage is getting high: "($ramusage.MemoryUsage)"%"
	exit 1
}