Table of Contents
We can have multiple use cases for getting memory usage percentages. For example:
- Getting overall memory usage percentage
- Getting overall memory usage percentage history
- Getting memory usage percentage for one/all processes
Let’s learn them one by one.
Overcall Memory Usage Percentage
We can use Get-Counter
and Get-WmiObject
cmdlets to retrieve the overall memory usage percentage. Let’s explore them below in detail.
Use Get-Counter
Cmdlet
Use the Get-Counter
cmdlet to get total memory usage in percentage.
1 2 3 4 5 |
$performance_counter = Get-Counter '\Memory\% Committed Bytes In Use' $usedMemoryPercentage = $performance_counter.CounterSamples[0].CookedValue Write-Host "Used Memory Percentage: $usedMemoryPercentage%" |
1 2 3 |
Used Memory Percentage: 57.7237514214878% |
We used the Get-Counter cmdlet to get the performance counter for the percentage of used memory bytes on the system and stored it in the $performance_counter
variable. The counter path '\Memory\% Committed Bytes In Use'
specified the memory counter that measured the proportion of committed memory bytes compared to the total physical memory available.
Then, we retrieved the performance counter’s value from $performance_counter
and assigned it to the $usedMemoryPercentage
variable. Now, how did we get the value of the performance counter? For that, we used the CounterSamples
property of the $performance_counter
variable, which contained an array of performance counter samples.
Since we intended to retrieve the first counter sample, we used [0]
to access it. Then, we used the CookedValue
property of the counter sample to get the message usage percentage. Finally, we used the Write-Host
to display it on the PowerShell console.
Use Get-WmiObject
Cmdlet
Use the Get-WmiObject
cmdlet to get the memory usage percentage if you use PowerShell version 3.0 or earlier.
1 2 3 4 5 6 7 |
$usedMemory = Get-WmiObject -Class Win32_OperatingSystem $totalMemory = $usedMemory.TotalVisibleMemorySize $freeMemory = $usedMemory.FreePhysicalMemory $usedMemoryPercentage = ($totalMemory - $freeMemory) / $totalMemory * 100 Write-Host "Used Memory Percentage: $usedMemoryPercentage%" |
1 2 3 |
Used Memory Percentage: 54.8795984097428% |
We used the Get-WmiObject cmdlet to get details about the OS, particularly the Win32_OperatingSystem
, which we specified using -Class
parameter. We stored the retrieved details in the $usedMemory
variable.
Then, we used the TotalVisibleMemorySize
and FreePhysicalMemory
properties of the $usedMemory
variable to get the visible memory size and free physical memory, respectively. We stored them in two separate variables named $totalMemory
and $freeMemory
.
After that, we calculated the percentage of the memory usage by subtracting the amount of $freeMemory
from the $totalMemory
, dividing the output by the $totalMemory
, then multiplying it by 100
. Finally, we stored this calculated percentage in the $usedMemoryPercentage
variable, which we further used with the Write-Host
cmdlet to print on the PowerShell console.
Prefer using Get-CimInstance cmdlet if you are using PowerShell version 3.0 or higher because the Get-WmiObject
has been superseded by Get-CimInstance
since PowerShell 3.0.
1 2 3 4 5 6 7 |
$usedMemory = Get-CimInstance -Class Win32_OperatingSystem $totalMemory = $usedMemory.TotalVisibleMemorySize $freeMemory = $usedMemory.FreePhysicalMemory $usedMemoryPercentage = ($totalMemory - $freeMemory) / $totalMemory * 100 Write-Host "Used Memory Percentage: $usedMemoryPercentage%" |
1 2 3 |
Used Memory Percentage: 54.8795984097428% |
Until now, we have learned how to get the overall memory usage percentage; what if you only want to know how much memory percentage has been consumed by the current process on your machine? Let’s see how we can do it.
Overcall Memory Usage Percentage History
Use the Get-Counter
cmdlet to get the overall memory usage percentage history.
1 2 3 |
Get-Counter '\Memory\% Committed Bytes In Use' -Continuous -SampleInterval 5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Timestamp CounterSamples --------- -------------- 7/15/2023 11:38:12 AM \\desktop-nkk10jm\memory\% committed bytes in use : 57.3812244593588 7/15/2023 11:38:17 AM \\desktop-nkk10jm\memory\% committed bytes in use : 57.3770478035736 7/15/2023 11:38:22 AM \\desktop-nkk10jm\memory\% committed bytes in use : 57.3751683014853 7/15/2023 11:38:27 AM \\desktop-nkk10jm\memory\% committed bytes in use : 57.3706575337263 |
We already learned the Get-Counter
cmdlet and counter path in the first example code. Here, we set the -SampleInterval
parameter to 5
to get performance counter data for the percentage of the committed memory bytes in use, with the sample interval of 5
seconds. However, the -Continous
parameter was used to continuously update the data until the user entered Ctrl+C from the keyboard to stop it.
Further reading:
Memory Usage Percentage for One/More Processes
We can use the Get-Process
and Get-WmiObject
cmdlets to find the memory usage percentage for one or all processes.
Use Get-Process
Cmdlet
Use the Get-Process
cmdlet to get the memory usage percentage for the current process on the local machine.
1 2 3 4 5 |
$current_process = Get-Process -Id $PID $usedMemoryPercentage = ($current_process.WorkingSet / (Get-WmiObject -Class Win32_ComputerSystem).TotalPhysicalMemory) * 100 Write-Host "Used Memory Percentage: $usedMemoryPercentage%" |
1 2 3 |
Used Memory Percentage: 0.918036721468859% |
We used the Get-Process
cmdlet with the -Id
parameter to get the information about the current process. We specified the current process using the $PID
variable, which contained the process ID (a.k.a PID) of the current PowerShell process.
By setting the -Id
with $PID
, we got the process details for the current PowerShell session, which we assigned to the $current_process
variable.
Then, we calculated the memory usage percentage and assigned it to the $usedMemoryPercentage
variable, which was used with Write-Host
to display on the console. But how did we calculate the memory usage percentage?
For that, we divided the WorkingSet
property of the $current_process
object (which contained the amount of memory the process was using) by the TotalPhysicalMemory
property of the Win32_ComputerSystem
WMI class (which contained the total amount of physical memory installed on the computer), and then multiplying the result by 100
.
This way, the above code retrieved the process object of the current process, calculated the memory usage percentage of that process, and then displayed the output on the PowerShell console.
Use Get-WmiObject
Cmdlet
Use the Get-WmiObject
cmdlet to get the memory usage percentage for all the processes on the local machine.
1 2 3 4 5 |
Get-WmiObject -Class Win32_PerfFormattedData_PerfProc_Process | Select-Object Name, IDProcess, @{Name="Memory Usage %";Expression={($_.WorkingSetPrivate / (Get-WmiObject Win32_ComputerSystem).TotalPhysicalMemory) * 100}} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Name IDProcess Memory Usage % ---- --------- -------------- ApplicationFrameHost 8556 0.0132102058275879 CalculatorApp 15368 0.00387112258683896 CompPkgSrv 14692 0.00532279355690357 CxMonSvc 3224 0.0582120058995908 CxUtilSvc 3248 0.00503245936289065 Greenshot 10684 0.0124359813102202 HPHotkeyNotification 15640 0.00832291356170376 HotKeyServiceUWP 3740 0.011323033566504 HxAccounts 10436 0.0434533510372673 HxOutlook 480 0.000435501291019383 HxTsr 5836 0.0515343194372936 Idle 0 9.67780646709739E-05 IntelCpHDCPSvc 1264 0 IntelCpHeciSvc 1384 0.00508084839522613 LanWlanWwanSwitchingServiceUWP 3104 0.0180007200288012 Locator 3504 0 Memory Compression 1888 3.33555277694979 MicTray64 2348 0.00479051420121321 ... ... ... ... ... ... ... ... ... |
The above code retrieved information about the currently running processes on the machine and calculated the memory usage percentage by each process. How?
First, we used the Get-WmiObject
cmdlet with Win32_PerfFormattedData_PerfProc_Process class to get performance counter date for all the processes running on the machine. Note that we used the -Class
parameter to specify the Win32_PerfFormattedData_PerfProc_Process
class; however, we can also omit this parameter.
Then, we piped the output, produced by the Get-WmiObject
, to the Select-Object
cmdlet to select the Name
, IDProcess
, and corresponding memory usage percentage. We calculated the memory percentage by dividing the process’s private working set by the computer’s total physical memory and then multiplying it by 100
.
Note that we used a HashTable to create a calculated property named Memory Usage %
.
That’s all about how to get memory usage percentage in PowerShell.