What It Does
The script finds the service you name, by its service name or its display name, stops it, starts it again and checks it is Running. It never changes the service's start type or settings. A stopped service is simply started.
If the service does not stop within TimeoutSeconds, the script ends the service's own process and says so in the answer. A service that shares its process with other running services is never killed, because that would stop the others too; the script reports an error instead.
With IncludeDependents, on by default, running services that depend on it are stopped with it and started again afterwards. The script refuses to restart the agent that is running it, and refuses the services of known RMM and remote-support agents unless Force is true.
Before You Run It
- Name the service exactly. Use the service name where two display names look alike.
- A running service is always restarted, so a second run restarts it again. Do not schedule it blind.
- If a service needs restarting every day, pair test-service-running with this and find out why it stops.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Stops and starts the named service and its running dependents; ends a hung service process.
- Restart: It never restarts the machine.
- Typical run: Under 3 minutes.
- Example answer: "Restarted Spooler (Print Spooler); Running"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- Name: The service name (for example Spooler) or the display name (for example Print Spooler). Required.
- TimeoutSeconds: How long to wait for the service to stop, and again to start. Whole number, 5 to 600. Default 60.
- IncludeDependents: true to stop running dependent services with it and start them again; false to fail instead. Default true.
- Force: true to allow restarting a known RMM or remote-support agent service. Never lets it restart the agent that is running this script. Default false.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.