How to add dependency on a Windows Service AFTER the service is installed

Solution 1:

This can also be done via an elevated command prompt using the sc command. The syntax is:

sc config [service name] depend= <Dependencies(separated by / (forward slash))>

Note: There is a space after the equals sign, and there is not one before it.

Warning: depend= parameter will overwrite existing dependencies list, not append. So for example, if ServiceA already depends on ServiceB and ServiceC, if you run depend= ServiceD, ServiceA will now depend only on ServiceD. (Thanks Matt!)

Examples

Dependency on one other service:

sc config ServiceA depend= ServiceB

Above means that ServiceA will not start until ServiceB has started. If you stop ServiceB, ServiceA will stop automatically.

Dependency on multiple other services:

sc config ServiceA depend= ServiceB/ServiceC/ServiceD/"Service Name With Spaces"

Above means that ServiceA will not start until ServiceB, ServiceC, and ServiceD have all started. If you stop any of ServiceB, ServiceC, or ServiceD, ServiceA will stop automatically.

To remove all dependencies:

sc config ServiceA depend= /

To list current dependencies:

sc qc ServiceA

Solution 2:

You can add service dependencies by adding the "DependOnService" value to the service in the registry using the regedit command, services can be found under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<Service name>. The details can be found at MS KB article 193888, from which the following is an excerpt from:

To create a new dependency, select the subkey representing the service you want to delay, click Edit, and then click Add Value. Create a new value name "DependOnService" (without the quotation marks) with a data type of REG_MULTI_SZ, and then click OK. When the Data dialog box appears, type the name or names of the services that you prefer to start before this service with one entry for each line, and then click OK.

Solution 3:

I was looking for a purely PowerShell (no regedit or sc.exe) method that can work on 2008R2/Win7 and newer, and came up with this:

Easy one is do the regedit with PowerShell:

Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation' -Name DependOnService -Value @('Bowser','MRxSmb20','NSI')

Or, using WMI:

$DependsOn = @('Bowser','MRxSmb20','NSI','') #keep the empty array element at end
$svc = Get-WmiObject win32_Service -filter "Name='LanmanWorkstation'"
$svc.Change($null,$null,$null,$null,$null,$null,$null,$null,$null,$null,$DependsOn)

The Change method of the Win32_Service class helped point to a solution:

uint32 Change(
[in] string  DisplayName,
[in] string  PathName,
[in] uint32  ServiceType,
[in] uint32  ErrorControl,
[in] string  StartMode,
[in] boolean DesktopInteract,
[in] string  StartName,
[in] string  StartPassword,
[in] string  LoadOrderGroup,
[in] string  LoadOrderGroupDependencies[],
[in] string  ServiceDependencies[]
);