Skip to content

$cs.Service_Exist

Checks whether a service with the given name is registered on the system, by looking for its key under the SYSTEM\CurrentControlSet\Services registry path.

$cs.Service_Exist(string servicename)

Name of the service to check for.

Boolean. $true if a registry key for the service is found, otherwise $false.

Check whether a service exists before stopping it:

Terminal window
if ($cs.Service_Exist("gupdate"))
{
$cs.Job_WriteLog("gupdate service found, stopping it")
$cs.Service_Stop("gupdate")
}

Verify a dependency service is registered before attempting to start it, and abort the install if it’s missing:

Terminal window
if (!$cs.Service_Exist("MySqlServerService"))
{
throw "Required dependency service MySqlServerService is not installed"
}
$cs.Job_WriteLog("Dependency service found, starting it")
$cs.Service_Start("MySqlServerService")

Only attempt cleanup of a service during uninstall if it was actually installed by this package:

Terminal window
if ($cs.Service_Exist("MyAppBackgroundService"))
{
$cs.Job_WriteLog("Removing MyAppBackgroundService")
$cs.Service_Stop("MyAppBackgroundService")
Start-Process "sc.exe" -ArgumentList "delete MyAppBackgroundService" -Wait
}
else
{
$cs.Job_WriteLog("MyAppBackgroundService not present, nothing to remove")
}

$cs.Service_Start $cs.Service_Stop