Display Memory Usage with PowerShell

There are several ways to get memory information. In this blog post I use WMI and the Get-CimInstance cmdlet. You can get basic memory statistics from the Win32_OperatingSystem class.

$os = Get-Ciminstance Win32_OperatingSystem
$os | select *memory*

As you can see in the example above, there are several memory-related properties.

The values are all in bytes. You can use these values to calculate a percentage of free memory. In this blog post we focused on physical memory.

I’m using the Round() method from the .NET [Math] class to round the value to two decimal places. We can now display the relevant information using Select-Object and a few custom properties.

$pctFree = [math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)
$os | Select @{Name = "PctFree"; Expression = {$pctFree}},
@{Name = "FreeGB";Expression = {[math]::Round($_.FreePhysicalMemory/1mb,2)}},
@{Name = "TotalGB";Expression = {[int]($_.TotalVisibleMemorySize/1mb)}}

Now we extended the basic concept with the function below:

Function Test-MemoryUsage {
[cmdletbinding()]
Param()

$os = Get-Ciminstance Win32_OperatingSystem
$pctFree = [math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)

if ($pctFree -ge 45) {
$Status = "OK"
}
elseif ($pctFree -ge 15 ) {
$Status = "Warning"
}
else {
$Status = "Critical"
}

$os | Select @{Name = "Status";Expression = {$Status}},
@{Name = "PctFree"; Expression = {$pctFree}},
@{Name = "FreeGB";Expression = {[math]::Round($_.FreePhysicalMemory/1mb,2)}},
@{Name = "TotalGB";Expression = {[int]($_.TotalVisibleMemorySize/1mb)}}

}

We used the If/ElseIf structure to evaluate the percentage of free memory and determine a status. Remember in an if statement, PowerShell stops checking after the first true evaluation, so we need to put the comparisons in ascending order.

We can even create an alias for it to save some typing.

Set-Alias -Name tmu -Value Test-MemoryUsage

But we want a bit more from this, perhaps something in color to highlight a critical condition. This should be simple enough. All we need to do is look at the status property and define an associated color.

$data = Test-MemoryUsage
Switch ($data.Status) {
 "OK" { $color = "Green" }
 "Warning" { $color = "Yellow" }
 "Critical" { $color = "Red" }
}

We not going to type that every single time. Instead we built a second function, which has the sole purpose of displaying the data from Test-MemoryUsage into a report form that suits my needs complete with its own alias.

Function Show-MemoryUsage {

[cmdletbinding()]
Param()

#get memory usage data
$data = Test-MemoryUsage

Switch ($data.Status) {
"OK" { $color = "Green" }
"Warning" { $color = "Yellow" }
"Critical" {$color = "Red" }
}

$title = @"

Memory Check
------------
"@

Write-Host $title -foregroundColor Cyan

$data | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor $color

}

set-alias -Name smu -Value Show-MemoryUsage

Normally we would not include any sort of formatting in a PowerShell function, nor would we use Write-Host to display results. But in this situation, that’s exactly what we want. The only purpose of this function is to show information on the screen.

We still have the first function if we need something in the pipeline. We put both functions and the Set-Alias commands in a PowerShell script and dot source it in your PowerShell profile.

. C:\Scripts\Check-Memory.ps1

We can also create a module. We can even extend these script to query remote computers. We can include the computername in the output and even a timestamp. We could use the test function if we need to export the memory data to a CSV file or process it further in PowerShell. We can also use the show function to display information to the screen.

Download

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s