Skip to content

$cs.Service_Stop

Stops the named service and waits up to maxTimeout milliseconds for it to reach the Stopped state. Unlike $cs.Service_Start, if the service doesn’t exist at all this returns silently with no error — it does not throw.

$cs.Service_Stop(string servicename, uint maxTimeout = 60000)

Name of the service to stop.

Maximum time to wait, in milliseconds, for the service to reach the Stopped state. Defaults to 60000 (60 seconds).

None.

Check the service exists before stopping it:

Terminal window
if ($cs.Service_Exist("gupdate"))
{
$cs.Job_WriteLog("Stopping gupdate service")
$cs.Service_Stop("gupdate", 30000)
}

Stop a service before replacing its binary during an in-place upgrade, then start it again afterward:

Terminal window
$cs.Job_WriteLog("Stopping MyAppService before replacing binaries")
$cs.Service_Stop("MyAppService", 60000)
Copy-Item -Path "$global:Packageroot\MyAppService.exe" -Destination "$env:ProgramFiles\MyApp\MyAppService.exe" -Force
$cs.Job_WriteLog("Starting MyAppService with updated binary")
$cs.Service_Start("MyAppService")

Call it defensively during uninstall without first checking existence — it silently does nothing if the service was never installed:

Terminal window
$cs.Job_WriteLog("Ensuring MyAppService is stopped before uninstall, in case it exists")
$cs.Service_Stop("MyAppService")

$cs.Service_Exist $cs.Service_Start