0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Check Memory Usage Easily

Use Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object TotalVisibleMemorySize,FreePhysicalMemory to get total and free memory, then calculate used memory in PowerShell.
📋

Examples

InputRun script on a system with 8GB RAM and 3GB free
OutputTotal Memory: 8192000 KB, Free Memory: 3072000 KB, Used Memory: 5120000 KB
InputRun script on a system with 16GB RAM and 10GB free
OutputTotal Memory: 16384000 KB, Free Memory: 10240000 KB, Used Memory: 6144000 KB
InputRun script on a system with 4GB RAM and 500MB free
OutputTotal Memory: 4096000 KB, Free Memory: 512000 KB, Used Memory: 3584000 KB
🧠

How to Think About It

To check memory usage, first get total and free physical memory from the system. Then subtract free memory from total memory to find used memory. This gives a clear picture of how much memory is currently in use.
📐

Algorithm

1
Get total visible memory size from the system.
2
Get free physical memory from the system.
3
Calculate used memory by subtracting free memory from total memory.
4
Display total, free, and used memory values.
💻

Code

powershell
 $mem = Get-CimInstance -ClassName Win32_OperatingSystem
$total = $mem.TotalVisibleMemorySize
$free = $mem.FreePhysicalMemory
$used = $total - $free
Write-Output "Total Memory: $total KB"
Write-Output "Free Memory: $free KB"
Write-Output "Used Memory: $used KB"
Output
Total Memory: 16777216 KB Free Memory: 8388608 KB Used Memory: 8388608 KB
🔍

Dry Run

Let's trace the script on a system with 16,777,216 KB total memory and 8,388,608 KB free memory.

1

Get total memory

TotalVisibleMemorySize = 16777216 KB

2

Get free memory

FreePhysicalMemory = 8388608 KB

3

Calculate used memory

Used Memory = 16777216 - 8388608 = 8388608 KB

Total Memory (KB)Free Memory (KB)Used Memory (KB)
1677721683886088388608
💡

Why This Works

Step 1: Get memory info

The Get-CimInstance command fetches system memory details from the Win32_OperatingSystem class.

Step 2: Calculate used memory

Subtracting FreePhysicalMemory from TotalVisibleMemorySize gives the amount of memory currently in use.

Step 3: Display results

The script prints total, free, and used memory in kilobytes for easy understanding.

🔄

Alternative Approaches

Using Get-WmiObject
powershell
 $mem = Get-WmiObject -Class Win32_OperatingSystem
$total = $mem.TotalVisibleMemorySize
$free = $mem.FreePhysicalMemory
$used = $total - $free
Write-Output "Total Memory: $total KB"
Write-Output "Free Memory: $free KB"
Write-Output "Used Memory: $used KB"
This uses an older cmdlet <code>Get-WmiObject</code> which is less efficient and deprecated in newer PowerShell versions.
Using Performance Counters
powershell
 $free = (Get-Counter '\Memory\Available Bytes').CounterSamples.CookedValue
$total = (Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory
$used = $total - $free
Write-Output "Total Memory: $([math]::Round($total / 1KB)) KB"
Write-Output "Free Memory: $([math]::Round($free / 1KB)) KB"
Write-Output "Used Memory: $([math]::Round($used / 1KB)) KB"
This method uses performance counters and reports memory in bytes, requiring conversion to KB.

Complexity: O(1) time, O(1) space

Time Complexity

The script runs a fixed number of system queries and calculations, so it executes in constant time.

Space Complexity

It uses a few variables to store memory values, so space usage is constant.

Which Approach is Fastest?

Using Get-CimInstance is faster and more modern than Get-WmiObject. Performance counters add complexity but provide more detailed info.

ApproachTimeSpaceBest For
Get-CimInstanceO(1)O(1)Simple and modern memory check
Get-WmiObjectO(1)O(1)Legacy systems or older scripts
Performance CountersO(1)O(1)Detailed memory stats in bytes
💡
Use Get-CimInstance instead of deprecated cmdlets for better performance and compatibility.
⚠️
Beginners often forget to subtract free memory from total memory, showing only one value instead of used memory.