Batch Findstr command


Wondering how I can enumerate more information for a line. Take for example, finding the status of a service via sc query | FINDstr /I "AppIDsvc"

It's pretty useless when it only shows "AppIDsvc" ha.

Any way I can make it pull me a few lines underneath?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Unfortunately, findstr doesn't offer a context like Select-String in Powershell does.
Well, for starters, you can always query a single service, which will give you only the status for this service:
sc.exe query "AppIDsvc"

Open in new window

Or combined with find to just show the state:
sc.exe query "AppIDsvc" | find.exe "STATE"

Open in new window

The best solution depends on what exactly it is you need.
Or do you need that because you only know parts of the service name, or want some specific output format?
Do you need this rather for interactive use, or inside another script?
Bill PrewIT / Software Engineering ConsultantCommented:
Another option with a little bit more info can be WMIC too, like:

wmic service where name="AppIDsvc" get * /format:list

Open in new window

For the fun of it, here's a batch script that lists the state of all services, optionally filtered by what findstr considers to be a Regular Expression.
@echo off
setlocal enabledelayedexpansion
if "%~1"=="/?" goto Syntax
if "%~1"=="-?" goto Syntax
if /i "%~1"=="-help" goto Syntax
if /i "%~1"=="/help" goto Syntax
if "%~1"=="" (
	set Pattern=.*
) else (
	set Pattern=%~1
set /a Index = 0
for /f "tokens=1 delims==" %%a in ('set Service[ 2^>NUL') do (set %%a=)
for /f "tokens=1* delims=: " %%a in ('sc.exe query state^= all bufsize^= 16384') do (
	if "%%a"=="SERVICE_NAME" (set /a Index += 1&set Service[!Index!].Name=%%b)
	if "%%a"=="DISPLAY_NAME" (set Service[!Index!].DisplayName=%%b)
	if "%%a"=="STATE" (for /f "tokens=2" %%s in ("%%b") do (set Service[!Index!].State=%%s))
for /l %%i in (1, 1, %Index%) do (
	echo !Service[%%i].Name!| findstr.exe /i /r /c:"%Pattern%" >NUL
	if not errorlevel 1 (
		echo !Service[%%i].DisplayName! ^(!Service[%%i].Name!^): !Service[%%i].State!
goto :eof
echo Syntax: %~nx0 [^<'Regular Expression' according to findstr.exe^>]
echo Notes: 
echo * to use the 'string start' anchor, use two '^^', and enclose the RE in double quotes.
echo * to query for an exact name, 'anchor' the expression to begin ('^^') and end ('$') of the name:
echo %~nx0 "^^wuauserv$"
echo Examples:
echo List all services with 'svc' in the name:
echo %~nx0 "svc"
echo List all services starting with 'w':
echo %~nx0 "^^W.*"
echo List all services ending with 'c':
echo %~nx0 ".*c$"

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

snyderkvAuthor Commented:
Thanks great info. I'll assign points but to answer your Q, I was using psexec to remote to a machine and use the | more command so I can just scroll through. Unfortunately, psexec sees | as a special character and bombs out. Hence the findstr

If you know how I can use | within psexec, let me know for cool factor

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snyderkvAuthor Commented:
Thanks Bill, didn't see the checkbox and missed you but I requested attention
snyderkvAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.