How to Get Memory Usage in PowerShell
!powershell-get-memory-usage
To get memory usage in PowerShell, you can use the Get-Process cmdlet and the WorkingSet property which provides the amount of memory used by the process in bytes.
The following syntax shows how you can do it.
Get-Process | Sort-Object -Property WorkingSet -Descending | Select-Object -First 10 -Property Id, Name, @{Name='Memory (MB)';Expression={$_.WorkingSet / 1MB}}
In this script, the Get-process cmdlet retrieves all running processes on the local computer and pipes them to the Sort-Object cmdlet to sort the processes by memory usage (WorkingSet) property in descending order.
It then selects the top 10 memory-consuming processes from the sorted list. Finally, it converts the WorkingSet into megabytes.
After running this script, it will display the top 10 processes that are consuming high memory in your local computer.
Understanding Memory Properties
PowerShell provides several memory-related properties for processes:
- WorkingSet: The amount of physical memory allocated to a process
- VirtualMemorySize: The amount of virtual memory allocated
- PrivateMemorySize: The amount of private memory allocated
Get-Process | Select-Object Name, @{N='WorkingSet(MB)';E={[math]::Round($_.WorkingSet/1MB,2)}}, @{N='Private(MB)';E={[math]::Round($_.PrivateMemorySize/1MB,2)}} | Sort-Object 'WorkingSet(MB)' -Descending | Select-Object -First 10
Conclusion
I hope the above article about getting memory usage in the local computer using PowerShell is helpful to you.
Related Articles
Process Monitoring
- PowerShell Get-Process - Get all process information
- PowerShell Get CPU Usage - Monitor CPU utilization
- PowerShell Get Process Name - Extract process names
- PowerShell Get-Service - Monitor services
Data Manipulation & Filtering
- PowerShell Select-Object - Select memory properties
- PowerShell Where-Object - Filter by memory threshold
- PowerShell Sort-Object - Sort by memory usage
- PowerShell ForEach-Object - Process each item
- PowerShell Measure-Object - Calculate memory statistics
Data Structures
- PowerShell Arrays - Store process memory data
- PowerShell Hashtables - Create memory data structures
- PowerShell Variables - Store memory values
Calculations & Formatting
- PowerShell DateTime Format - Format data values
- PowerShell Format Table - Display memory data
Control Flow & Logic
- PowerShell If-Else Statement - Conditional memory checking
- PowerShell Switch Statement - Switch-based thresholds
- PowerShell Try-Catch - Error handling
Output & Export
- PowerShell Output to File - Save memory reports
- PowerShell Export CSV - Export memory data
- PowerShell Format Table - Display formatted output
- PowerShell Output Table - Create output tables
Functions & Automation
- PowerShell Functions - Create memory monitoring functions
- PowerShell Add-Member - Add custom memory properties
Comprehensive Guides
- Complete PowerShell Guide - Full PowerShell with system monitoring
- Complete PowerShell Tutorial - Comprehensive course