Get Memory Usage Percentage in PowerShell

PowerShell get Memory Usage Percentage

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.

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.

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.

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.

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.

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.

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.

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.

Was this post helpful?

Leave a Reply

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