JSI Tip 9136. How can a script return the service display name and service short name, sorted by service display name?

I have scripted SvcDnSnRpt.bat to return the service display name and service short name, sorted by service display name.

The syntax for using SvcDnSnRpt.bat is:

for /f "Tokens=1* Delims=," %%s in ('SvcDnSnRpt') do (
 call :DoSomething %%s %%t


To create a .CSV file, you might script the following:
@echo off
if exist FileName.csv del /q FileName.csv
for /f "Tokens=1* Delims=," %%s in ('SvcDnSnRpt') do (
 call :DoSomething %%s %%t
goto :EOF
@echo %1,%2>>FileName.csv
The first few records in FileName.csv might contain:
"Application Layer Gateway Service","ALG"
"Application Management","AppMgmt"
"ASF Agent","ASFAgent"
"ASP.NET State Service","aspnet_state"
"Automatic Updates","wuauserv"
"Background Intelligent Transfer Service","BITS"
"CA License Client","CA_LIC_CLNT"
"CA License Server","CA_LIC_SRVR"
"COM+ Event System","EventSystem"
"COM+ System Application","COMSysApp"
"Computer Browser","Browser"
NOTE: The service short name is the registry sub-key at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services.

SvcDnSnRpt.bat contains:

@echo off
set wrk=%TEMP%\SvcDnSnRpt_%RANDOM%
set vbs="%wrk%.VBS"
set si="%wrk%_SI.TMP"
set so="%wrk%_SO.TMP"
set wrk=                                             #
set wrk=%wrk:~0,40%
set blank=%wrk%%wrk%
@echo Dim strServerName, oServiceSet, oWshNetwork>%vbs%
@echo strServerName = "LocalHost">>%vbs%
@echo Set oServiceSet = GetObject("winmgmts:\{impersonationLevel=impersonate\}!//" ^& strServerName ^& "/root/cimv2").InstancesOf("Win32_Service")>>%vbs%
@echo Set oWshNetwork = WScript.CreateObject("WScript.Network")>>%vbs%
@echo Set oWshNetwork = Nothing>>%vbs%
@echo For each Service in oServiceSet>>%vbs%
@echo   WScript.Echo Service.DisplayName ^& "," ^& Service.Name>>%vbs%
@echo Next>>%vbs%
@echo Set oServiceSet = Nothing>>%vbs%
for /f "Tokens=1* Delims=," %%a in ('cscript.exe //NOLOGO %vbs%') do (
 set dn=%%a
 set sn=%%b
 call :sortin
sort %si% /O %so%
del /q %vbs%
del /q %si%
for /f "Tokens=2,3 Delims=," %%a in ('type %so%') do (
 @echo "%%a","%%b"
del /q %so%
goto :EOF
set wrk=%dn%%blank%
set sf=%wrk:~0,80%
@echo %sf%,%dn%,%sn%>>%si%

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.