I want to add via Batch script a new service-dependency to an existing service with old dependencies, without deleting these dependencies.

I know the command sc config ServiceA depend= ServiceB/ServiceC/ServiceD can add multiple dependencies, but I don't know how to use it to - for example - add ServiceD dependency to a service that depends on ServiceB and ServiceC, with the same result and without mentioning the old dependencies (since I don't know them).

What is the way to do that? And - how to revert the change (also via Batch)?

I have no much experience with Batch, by the way.


Find out the existing dependencies, add your new one and write the whole thing back, say you have a service called w3svc, you can use:

sc.exe  qc  w3svc

or in PowerShell:

(get-service w3svc | Select ServicesDependedOn).ServicesDependedOn

where you can loop through these and build a new sc.exe command to execute.


 $serviceName = "w3svc"
 $cmd = "config $serviceName depend= "
 (gsv $serviceName | Select ServicesDependedOn).ServicesDependedOn | % {$cmd += $_.Name + "/"}
 $cmd += "myMasterService"
 invoke-expression "sc.exe $cmd"

We are building a command string from the existing services and add your own.

To remove yours do the same thing, but in the loop exclude your service.

 $serviceName = "w3svc"
 $cmd = "config $serviceName depend= "
 (gsv $serviceName | Select ServicesDependedOn).ServicesDependedOn | % {
      if ($_.Name -ne "myMasterService")
           $cmd += $_.Name + "/"
 # remove the last slash
 $cmd = $cmd -replace "/$", ""   
 invoke-expression "sc.exe $cmd"

I haven't tested the actual sc.exe commands, but I assume you know what you are doing.

| improve this answer | |
  • Thanks! Can you write the Batch commands to run if I don't want to use PowerShell? It will really really help! – Reflection Mar 4 '15 at 18:16
  • 1
    Sorry, it's 2015, not the 1980s. I haven't done batch files in a long time and doing this in a batch would be much more difficult anyway. – Peter Hahndorf Mar 4 '15 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.