Skip to main content

How to List Processes by Memory Usage in Batch Script

Identifying which applications are consuming the most system memory is a critical step in performance troubleshooting and system diagnostics. When a computer is slow, a "memory hog" is often the culprit. While the graphical Task Manager is useful for this, a command-line tool is essential for scripting, automated monitoring, and remote diagnostics. The standard, built-in utility for this task is tasklist.exe.

This guide will teach you how to use the tasklist command with its built-in sorting capabilities to list all running processes from highest to lowest memory usage. You will also learn how to capture and parse this sorted list to create a "Top N" report of the most memory-intensive processes.

The Core Command: tasklist /SORT

The tasklist.exe utility includes a powerful /SORT switch that allows you to order the output based on different criteria. For our purpose, the key value is MEMUSAGE.

Syntax: tasklist /SORT <Criteria> [/DESC]

  • /SORT: The switch to enable sorting.
  • <Criteria>: The column to sort by. We will use MEMUSAGE.
  • /DESC: An optional switch to sort in DESCending order (highest to lowest). This is usually what you want for finding memory hogs.

Sorting by Memory Usage (Descending)

To get the list of processes from the most memory-hungry to the least, you combine the sort criteria with the descending switch.

@ECHO OFF
ECHO --- Listing all processes, sorted by memory usage (highest first) ---
ECHO.
tasklist /SORT MEMUSAGE /DESC

The output is a standard tasklist table, but it is now ordered by the "Mem Usage" column, making it easy to spot the top consumers at a glance.

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
chrome.exe 8124 Console 1 1,234,567 K
msedge.exe 9876 Console 1 876,543 K
Code.exe 1234 Console 1 543,210 K
svchost.exe 1122 Services 0 98,765 K
... (and many more) ...

The Modern Alternative: Using PowerShell

For modern systems, PowerShell provides a more flexible and powerful way to get and sort process information.

Syntax: powershell -Command "Get-Process | Sort-Object -Property WorkingSet -Descending"

  • Get-Process: The cmdlet to get all running processes.
  • | Sort-Object ...: Pipes the list of processes to the sorting cmdlet.
  • -Property WorkingSet: WorkingSet is the PowerShell equivalent of "Mem Usage".
  • -Descending: Sorts from highest to lowest.

This method is powerful because you can easily select other properties to display: powershell -Command "Get-Process | Sort-Object WS -Desc | Select-Object -First 10 Name, WS"

How to Capture and Parse the Sorted List in a Script

To use the sorted list in a script (e.g., to create a report), you need to capture the output. The best way to do this is to have tasklist format the output as CSV (Comma-Separated Values), which is easy to parse with a FOR /F loop.

@ECHO OFF
ECHO --- Top Processes by Memory ---
ECHO.

REM /FO CSV: Format as CSV
REM /NH: No Header
REM /SORT MEMUSAGE /DESC: The sorting command
FOR /F "tokens=1,5 delims=," %%A IN (
'tasklist /FO CSV /NH /SORT MEMUSAGE /DESC'
) DO (
ECHO Process: %%~A - Memory: %%~B
)
  • tokens=1,5: In the CSV output, the Image Name is the 1st token and Memory Usage is the 5th.
  • %%~A and %%~B: The tilde (~) removes the quotes that CSV format adds.

Common Pitfalls and How to Solve Them

Problem: Not All Processes are Visible (Administrator Rights)

If you run tasklist as a standard user, you will only see the processes running under your own user account. You will miss all the system services and processes running under other accounts, which can be significant memory consumers.

Solution: For a complete and accurate report of all processes on the system, you must run the script as an Administrator.

Problem: The Default Table Format is Hard to Parse

The default TABLE format uses a variable number of spaces to align its columns. This makes it extremely difficult to parse reliably with a FOR /F loop.

Solution: Always use the /FO CSV switch when you intend for your script to process the output of tasklist. It provides a clean, predictable format that is perfect for parsing.

Practical Example: A "Top 10 Memory Hogs" Report Script

This script uses the techniques learned to create a clean report of the top 10 most memory-intensive processes currently running.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "LIMIT=10"
SET "count=0"

ECHO --- Top %LIMIT% Processes by Memory Usage ---
ECHO.
ECHO Image Name Memory Usage
ECHO ======================== =================
ECHO.

REM Use the sorted, CSV, no-header command for clean parsing.
FOR /F "tokens=1,5 delims=," %%A IN (
'tasklist /FO CSV /NH /SORT MEMUSAGE /DESC'
) DO (
SET /A "count+=1"
IF !count! LEQ %LIMIT% (
REM Pad the process name to a fixed width for nice alignment
SET "ImageName=%%~A "
SET "ImageName=!ImageName:~0,25!"
ECHO !ImageName! %%~B
)
)

ENDLOCAL

Conclusion

The tasklist command is the definitive built-in tool for listing and sorting processes from the command line, making it perfect for batch scripting.

Key takeaways for creating memory usage reports:

  • Use tasklist /SORT MEMUSAGE to order the processes by memory.
  • Add the /DESC switch to sort from highest to lowest, which is the most common use case.
  • For scripting, always use the /FO CSV switch to produce clean, parsable output.
  • Run your script as an Administrator to get a complete list of all processes on the system.
  • Combine these commands with a FOR /F loop and a counter to create powerful "Top N" diagnostic reports.