$cs.Service_Stop
Description
Section titled “Description”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.
Syntax
Section titled “Syntax”$cs.Service_Stop(string servicename, uint maxTimeout = 60000)
Parameters
Section titled “Parameters”servicename (String)
Section titled “servicename (String)”Name of the service to stop.
maxTimeout (UInt32)
Section titled “maxTimeout (UInt32)”Maximum time to wait, in milliseconds, for the service to reach the Stopped state. Defaults to 60000 (60 seconds).
Return value
Section titled “Return value”None.
Example
Section titled “Example”Check the service exists before stopping it:
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:
$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:
$cs.Job_WriteLog("Ensuring MyAppService is stopped before uninstall, in case it exists")$cs.Service_Stop("MyAppService")