Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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%
like image 429
user2771150 Avatar asked Nov 26 '13 04:11

user2771150


1 Answers

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
like image 184
unclemeat Avatar answered Oct 25 '22 07:10

unclemeat