Disable Services in bulk via batch file.

Discussion in 'Software' started by Rayzipan, Jun 28, 2015.

  1. Rayzipan

    Rayzipan Specialist

    Hi MGF.

    I'd like to disable many services in bulk. Can this be done via a batch file?

    For example in service_manual.bat - I'd like to Stop and set to Manual these services.
    • Adaptive Brightness
    • Application Experience
    • BitLocker Drive Encryption Service
    • Bluetooth Support Service
    • Certificate Propagation

    For example in service_disable.bat - I'd like to Stop and set to Disable these services.
    • Diagnostic Policy Service
    • Diagnostic System Host
    • Disk Defragmenter
    • Distributed Link Tracking Client
    • Fax

    Going through many services to disable and or set to manual is very tedious.

    Thanks.
     
  2. Earthling

    Earthling Interplanetary Geek

    Frankly, you could have manually disabled all of those in the time it took you to frame the question, not to mention the time you will waste dealing with replies.
     
  3. Rayzipan

    Rayzipan Specialist

    Those 10 are just a sample from a long list. A batch file would be handy for future use.
     
  4. foogoo

    foogoo Major "foogoo" Geek

  5. GermanOne

    GermanOne Guest

    Ray

    The command line tool you are looking for is SC.
    Even with this command its a little tricky because your list contains the "display names" instead of the real service names. Nevermind - Also the display names can be resolved using SC.

    I'd suggest you to save all the services into a list file, eg. "service_manual.txt". Make sure there is no additional space at the end of the lines.

    .bat
    Code:
    @echo off &setlocal DisableDelayedExpansion
    set "listfile=service_manual.txt"
    set "starttype=demand"
    
    cd /d "%~dp0"
    for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
    >nul chcp 1252
    
    for /f "delims=" %%i in ('type "%listfile%" ^&^>nul chcp %oemcp%') do (
      set "displayname=%%i"
      for /f "tokens=1* delims== " %%j in ('sc getkeyname "%%i"^|findstr /bic:"name = "^|^|echo failed') do set "servicename=%%k"
      setlocal EnableDelayedExpansion
      if not defined servicename (
        echo unable to resolve "!displayname!" to the service name
      ) else (
        >nul sc config "!servicename!" start= %starttype%
        if errorlevel 1 (
          echo unable to change the configuration of "!displayname!"
        ) else (
          echo configuration of "!displayname!" successfully changed
        )
      )
      endlocal
    )
    
    Remarks:
    • Either you save both the list file and your Batch file in the same folder, or you have to define a fully qualified path in variable "listfile" (line 2).
    • Define value demand for "manually started", or disabled for "starting deactivated" in variable "starttype" (line 3).
    • The Batch process has to be elevated (right-click and "Run as Administrator").
    Hope that helps :)

    Regards
    GermanOne
     
  6. Rayzipan

    Rayzipan Specialist

  7. Rayzipan

    Rayzipan Specialist

    Thank you GermanOne.

    Does your code also stop the services?

    It did not work. I tried both Print Spooler and just Spooler in 'service_manual.txt'.
     
  8. GermanOne

    GermanOne Guest

    No, it doesn't. I didn't even realize that it was one of the requirements. Seems I didn't read carefully enough :(
    Code:
    @echo off &setlocal DisableDelayedExpansion
    set "listfile=service_manual.txt"
    set "starttype=demand"
    
    cd /d "%~dp0"
    for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
    >nul chcp 1252
    
    for /f "delims=" %%i in ('type "%listfile%" ^&^>nul chcp %oemcp%') do (
      set "displayname=%%i"
      for /f "tokens=1* delims== " %%j in ('sc getkeyname "%%i"^|findstr /bic:"name = "^|^|echo failed') do set "servicename=%%k"
      setlocal EnableDelayedExpansion
      if not defined servicename (
        echo unable to resolve "!displayname!" to the service name
      ) else (
        >nul sc stop "!servicename!"
        if errorlevel 1 (
          echo unable to stop service "!displayname!"
        ) else (
          >nul sc config "!servicename!" start= %starttype%
          if errorlevel 1 (
            echo unable to change the configuration of "!displayname!"
          ) else (
            echo service "!displayname!" stopped and configuration successfully changed
          )
        )
      )
      endlocal
    )
    pause
    
    Also appended the PAUSE command. Now you should be able to observe the messages.

    Regards
    GermanOne
     
  9. Rayzipan

    Rayzipan Specialist

    Hi GermanOne.

    Sorry for the delayed reply. I have been crazy busy of late. I tried your latest code. After a reboot, the service is stopped and set to manual.

    Thanks again for your help.
     
  10. theefool

    theefool Geekified

    A bit of powershell:

    Get-service service_manual.txt | Stop-Service
    Set-service service_manual.txt -startuptype "manual"

    Haven't tried the above, nor the below:

    Get-service service_manual.txt | {
    Stop-Service
    Set-service -startuptype "manual"
    }
     
    Last edited: Jul 18, 2015
  11. Rayzipan

    Rayzipan Specialist

    Hi theefool.

    Now that's what I call streamlined.
     
  12. theefool

    theefool Geekified

    Note the "I haven't tried that". I'm finding that powershell is awesome and sometimes easier to use than dos .bat files. I prefer dos, but as I progress, I'm finding powershell easier to use. Somewhat.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds