PowerShell – get memory usage

Posted by antonello on Mon, 21 Feb 2022 23:56:42 +0100

Many times, PowerShell administrators must manually scan the server to see the performance problems of the server in the network. This can happen when one of the servers is not responding or unresponsive and causes performance problems on the related servers. In this case, knowing the memory usage of the server will help determine which particular server has higher memory usage.

Gets the memory usage on the PowerShell getiobject computer. There are many ways to PowerShell Gets the memory utilization on the local or remote computer.

In this article, I will explain how to use PowerShell to get the memory usage of the server.

Catalogue hide 

PowerShell get memory usage

To use PowerShell to get the memory usage of the first 10 processes on the local computer, use the following command

PS D:\PowerShell> Get-WmiObject WIN32_PROCESS | Sort-Object -Property ws -Descending | Select-Object -first 10 ProcessID,Name,WS

ProcessID Name                      WS
--------- ----                      --
     2628 Memory Compression 909996032
    11984 chrome.exe         470863872
    15728 firefox.exe        356192256
    14444 chrome.exe         332091392
    16680 firefox.exe        315645952
    11812 chrome.exe         311554048
    22072 Teams.exe          242507776
    11256 firefox.exe        221163520
    27052 powershell_ise.exe 213434368
    25612 chrome.exe         211025920

In the PowerShell script above,

  • Using the get wmioobject cmdlet and WIN32_Process class, which can get information about all processes on the local computer.
  • Sort all processes in WS descending order
  • Use select object to obtain the first 10 process IDs, process names and WS
  • It gets the memory usage of the first 10 processes

PowerShell get memory utilization (local)

As you can see from the above information, we know which processes have high memory usage, but it's hard to know what percentage of memory these processes will use.

Cool tip: how to create a resource pool to measure VM memory usage!

To get the percentage of total memory usage on the computer, we need to know how many TotalVisibleMemorySize and FreePhysicalMemory are available on the system.

Run the following command to get the process memory usage in MB on the local computer

 # Get Computer Object
 $CompObject =  Get-WmiObject -Class WIN32_OperatingSystem
 $Memory = ((($CompObject.TotalVisibleMemorySize - $CompObject.FreePhysicalMemory)*100)/ $CompObject.TotalVisibleMemorySize)
 
 Write-Host "Memory usage in Percentage:" $Memory
        
 # Top 5 process Memory Usage (MB)
 $processMemoryUsage = Get-WmiObject WIN32_PROCESS | Sort-Object -Property ws -Descending | Select-Object -first 5 processname, @{Name="Mem Usage(MB)";Expression={[math]::round($_.ws / 1mb)}}
 $processMemoryUsage

Output:

Memory usage in Percentage: 78.134547088066

ProcessName        Mem Usage(MB)
-----------        -------------
Memory Compression          1032
chrome.exe                   373
Teams.exe                    370
firefox.exe                  315
chrome.exe                   307

In the PowerShell script above,

  • Use get wmiobject - class Win32_ Operatingsystem gets the local computer object
  • Get information about TotalVisibleMemorySize and FreePhysicalMemory
  • Calculate and get the memory usage percentage
  • Using Win_32_Process gets all processes, sorted in ws descending order
  • Use select object to get the memory usage of the first five processes
  • It is easy to read and explain which process has higher memory utilization.

Cool tip: how to use in PowerShell Multiline command!

PowerShell gets the memory usage on the remote computer

To use PowerShell to get the memory usage of the first 10 processes on the remote computer, use the following command

PS D:\PowerShell> Get-WmiObject WIN32_PROCESS -ComputerName 'corp-in-18' | Sort-Object -Property ws -Descending | Select-Object -first 5 ProcessID,Name,WS
>>

ProcessID Name                      WS
--------- ----                      --
    12628 Memory Compression 913199104
    10522 chrome.exe         438820864
    17292 Outlook.exe          393461760
    26529 firefox.exe        354979840
    23133 chrome.exe         325996544

In the PowerShell script above,

  • Using the get wmioobject cmdlet and Win32_ The process class, which gets information about all processes on the remote computing name specified by the – ComputerName property
  • Sort all processes in WS descending order
  • Used to select object to obtain the first five process ID s, process names and WS
  • It gets the memory usage of the first five processes

To get the percentage of total memory usage on the remote computer, get the TotalVisibleMemorySize and FreePhysicalMemory available on the system.

Run the following command to get the process memory usage in mb on the remote computer

 $CompName  = 'corp-in-18'
 # Get Computer Object
 $CompObject =  Get-WmiObject -Class WIN32_OperatingSystem -ComputerName $CompName 
 $Memory = ((($CompObject.TotalVisibleMemorySize - $CompObject.FreePhysicalMemory)*100)/ $CompObject.TotalVisibleMemorySize)
 
  Write-Host "Memory usage in Percentage:" $Memory
 # Top 5 process Memory Usage (MB)
 $processMemoryUsage = Get-WmiObject WIN32_PROCESS -ComputerName $CompName  | Sort-Object -Property ws -Descending | Select-Object -first 5 processname, @{Name="Mem Usage(MB)";Expression={[math]::round($_.ws / 1mb)}}
 $processMemoryUsage

Output

Memory usage in Percentage: 77.2521027641826

ProcessName        Mem Usage(MB)
-----------        -------------
Memory Compression          1105
chrome.exe                   358
firefox.exe                  318
chrome.exe                   302
firefox.exe                  290

In the PowerShell script above,

  • Use the get wmiobject cmdlet to get the ComputerName of the remote computer object specified by
  • Get information about TotalVisibleMemorySize and FreePhysicalMemory
  • Calculate and get the memory usage percentage
  • Using Win_32_Process gets all processes on the remote computer, sorted in ws descending order
  • Use select object to get the memory usage of the first five processes

Cool tip: how to in PowerShell Add line breaks to strings or variables!

conclusion

I hope the above article on using PowerShell scripts to get memory usage on local or remote computers will help you get memory usage information and take action against it.

Using Win32_Process class, which returns all available processes on the computer specified by ComputerName.

Topics: Operation & Maintenance server