Get memory utilization report using PowerShell

A long time ago I added short article about checking CPU and Memory utilization. This time I wanted to create simple report with few additional details.

Memory utilization

Checking memory utilization can be done in multiple ways. This time I want to show you how to do this using Get-WMIObject command. Here you can see simple one-liner script to check top 5 processes:

Get-WmiObject WIN32_PROCESS | Sort-Object -Property ws -Descending | Select-Object -first 5 ProcessID,Name,WS
top 5 memory
top 5 memory

As you can see above output doesn’t look to pretty. This script can be used to check quickly one specific computer just by adding parameter -Computername to command – check technet site for more information.

What if we want to check multiple servers and have nice formatted output like this below?



The following script will check all machines from $Servers variable. First it checks if server is accessible using Test-Path command. Then it use Get-WMIObject to get memory utilization details. At then end for each server new object will be created and added to array.

    $Servers = Get-Content -Path "C:\users\$env:username\desktop\servers.txt"
    $Array = @()
    Write-Host "`n------------------------------------------------------" -ForegroundColor Green
    Write-Host "                Checking 'Memory usage'"
    Write-Host "------------------------------------------------------`n" -ForegroundColor Green
    ForEach($Server in $Servers)
        $Server = $Server.trim()
        Write-Host "$Server - " -ForegroundColor Green -NoNewline

        $TopMem = $null
        $Object = $null
        $Status = $null
        $ComputerMemory = $null
        $RoundMemory = $null

        $TestPath = Test-Path "\\$Server\c$"
        If($TestPath -match "False")
            Write-Host "ERROR:   Failed to connect"
            $Status = "Offline"
            $ProcessName = "(Null)"
            $ProcessMem = "(Null)"
            $ProcessUser = "(Null)"
            $RoundMemory = "(Null)"
            Write-Host "SUCCESS: Server is up"
            $Status = "Online"

            # Memory utilization
            $ComputerMemory =  Get-WmiObject -Class WIN32_OperatingSystem -ComputerName $Server 
            $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
            # Top process
            $TopMem = Get-WmiObject WIN32_PROCESS -ComputerName $Server | Sort-Object -Property ws -Descending | Select-Object -first 1 processname, @{Name="Mem Usage(MB)";Expression={[math]::round($ / 1mb)}},@{Name="UserID";Expression={$_.getowner().user}}

            If($TopMem -and $ComputerMemory)
                $ProcessName = $TopMem.ProcessName
                $ProcessMem  = $TopMem.'Mem Usage(MB)'
                $ProcessUser = $TopMem.UserID
                $RoundMemory = [math]::Round($Memory, 2)
                $ProcessName = "(Null)"
                $ProcessMem = "(Null)"
                $ProcessUser = "(Null)"
                $RoundMemory = "(Null)"
            # Creating custom object

            $Object = New-Object PSObject -Property ([ordered]@{ 
                            "Server name"             = $Server
                            "Status"                  = $Status
                            "Total usage %"           = $RoundMemory
                            "Top process"             = $ProcessName
                            "Top process usage (MB)"  = $ProcessMem
                            "Top process user"        = $ProcessUser

            # Add object to our array
            $Array += $Object   

    Write-Host "`nResults" -ForegroundColor Yellow

    # Display results in console
    $Array | Sort-Object status | Format-Table -AutoSize -Wrap
    # Display results in new window
    $Array | Out-GridView
    # Save to CSV file
    $Array | Export-Csv -Path C:\temp\results.csv -NoTypeInformation

I hope this was informative for you 🙂 See you in next articles.

Merry Christmas !!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.