$cs.Service_Exist
Description
Section titled “Description”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.
Syntax
Section titled “Syntax”$cs.Service_Exist(string servicename)
Parameters
Section titled “Parameters”servicename (String)
Section titled “servicename (String)”Name of the service to check for. Use the service name (for example, gupdate), not the display name shown in the Services console.
Return value
Section titled “Return value”Boolean. $true if a registry key for the service is found, otherwise $false.
Example
Section titled “Example”Check whether a service exists before stopping it:
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:
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:
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")}