How to get the current CPU usage and available memory in batch file?

user2771150 picture user2771150 · Nov 26, 2013 · Viewed 20k times · Source

I am creating a simple script that outputs the current user logged in, CPU usage for the current system and the available memory?

I have managed to get the current user/s logged on but is it possible to get the cpu usage and memory as well?

This is my code so far.

@echo off

for /f "tokens=3 delims=\" %%i in ("%USERPROFILE%") do (set USER=%%i) 2>&1  
echo "Logged On User: %USER%"  
echo. 

pause

To get the cpu usage i have tried this command but it doesn't seem to be working.

for /f "skip=1" %p in ('wmic cpu get loadpercentage') do 
echo %p%

Answer

unclemeat picture unclemeat · Nov 26, 2013

Firstly, when running from a batch file, the for loop variable needs two percentage symbols - %%p

Secondly, you need to echo %%p, not %p%:

for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do echo %%p

From the command line:

for /f "skip=1" %p in ('wmic cpu get loadpercentage') do echo %p