Originally posted on: http://geekswithblogs.net/BruceEitman/archive/2014/06/24/wes-use-command-line-to-disable-a-service.aspx
I have been working with a customer who needs to be able to
disable services in WES 7. They want to
do this after using IBW to install WES.
So, how can we manage services from the command line? The answer is use SC.EXE, the service
control manager. SC.EXE is capable of
doing many things, but since all we need to do is disable a service let’s focus
on that. Well, let’s also shutdown the
service, which may not really be necessary except that it will let us see what the
systems does with the service shutdown.
If you want to know all of the features of SC.EXE, simply
run “SC.EXE /?” to see a list. From
there, you can run “SC.EXE <Command>” to get more information on the
possible commands.
For this example, I will disable the Windows Update Service.
First, you will need the service name. To get the service name using SC.EXE, run “SC.EXE
query” which will output information about all services currently running. That is way too much information, so I
recommend redirecting the output to a file by running “SC.EXE query >out.txt”. Searching the output for Windows Update
finds:
SERVICE_NAME: wuauserv
DISPLAY_NAME: Windows Update
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 4 RUNNING
(STOPPABLE, NOT_PAUSABLE, ACCEPTS_PRESHUTDOWN)
WIN32_EXIT_CODE : 0
(0x0)
SERVICE_EXIT_CODE : 0
(0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
So now we know that the service name is wuauserv.
Now, disable a service.
To disable the service, we need to configure it, so we will use SC.EXE
config which has an option to change the start settings. Run “SC.EXE config wuauserv start= disable”
to disable the service. NOTE: There is a space between the ‘=’ and disable.
Finally, shutdown the service by running SC stop. This is simple, just run “SC.EXE stop
wuauserv”. You can confirm that the
service stopped by running “SC.EXE query wuauserv”, whih will output:
SERVICE_NAME: wuauserv
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 1 STOPPED
WIN32_EXIT_CODE : 0
(0x0)
SERVICE_EXIT_CODE : 0
(0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
But, it would be even better to put this into a batch
file. Here it is:
@echo off
call :DisableService wuauserv
goto :EOF
:DisableService
sc config %1 start=
disabled
sc stop %1
goto :EOF
Which seems like a lot of work to disable only one service,
so let’s make it do several services by reading a list from a file:
@echo off
set ServiceListFile=servicelist.txt
FOR /F %%i in (%ServiceListFile%) DO (
call :DisableService %%i
)
goto :EOF
:DisableService
sc config %1 start= disabled
sc stop %1
goto :EOF
And servicelist.txt might look like:
ShellHWDetection
CISVC
IISADMIN
wuauserv
Now we can disable and shutdown a list of services after
running IBW.