Ask Rich: if I wanted to perform math on the output of a cmd line string… can I do it?

I was asked if it was possible to perform math on the output of command line string. Can it be done?

Sure.

Example command given:

C:WindowsSystem32>wmic os get TotalVirtualMemorySize /every:5 /repeat:5 | findstr /r [0-9]

This returns a value, in my case, of: 4800564

Wants to divide by 1024 and check ever 5 seconds for 5 iterations. Here’s my adapted batch script.

@echo off
@setlocal enableextensions enabledelayedexpansion
set max_iterations=5
set iteration=1

:loop
if %iteration% GTR %max_iterations% (goto end)
for /f tokens=1,* delims== %%x in ('wmic os get TotalVirtualMemorySize ^| findstr /r ^[0-9]^') do set /A var=%%x/1024
echo %var%
goto sleep

:sleep
ping -n 5 127.0.0.254 >NUL 2>&1
set /A iteration=%iteration%+1
goto loop

:end
echo bye

askrich1

Get Application GUID Using WMIC

Here is a quick command to get all the application GUID’s on the current system using WMIC command and put them into a CSV for import into Excel.

wmic product get identifyingnumber,name,vendor,version /FORMAT:csv | find "{" >>%COMPUTERNAME%-GUID.csv

wmic output hangs when using psexec

I recently found when trying to issue a wmic query on a remote system with psexec (1.98) it would hang and not display results.

The solution is to redirect STDIN to NUL.

psexec computer cmd /c "wmic path win32_usbcontroller < NUL:"

I found this solution on Sysinternals forums from June 2011.

Get Process CPU Usage Using WMI

I recently blogged about another method to obtain a process’ CPU usage via Command Line: Get CPU Usage of a Process from Command Line

Here’s a quick command line method for grabbing CPU usage of a process by process id, process name, or caption.

By Process ID

wmic path win32_perfformatteddata_perfproc_process where (IDProcess = '3488') get Name, Caption, PercentProcessorTime, IDProcess /format:list

byprocid

By Process Name

wmic path win32_perfformatteddata_perfproc_process where (Name='iexplore') get Name, Caption, PercentProcessorTime, IDProcess /format:list

byprocname

By Process Name (fuzzy)

wmic path win32_perfformatteddata_perfproc_process where (Name like '%iexp%') get Name, Caption, PercentProcessorTime, IDProcess /format:list

byprocnamefuzzy

Any processes with CPU time over 50%

wmic path win32_perfformatteddata_perfproc_process where (PercentProcessorTime ^> 50) get Name, Caption, PercentProcessorTime, IDProcess /format:list

bycputime