POLL: POWERSHELL VS. GUI - DO YOU WANT TO BE A DEVOP OR AN ADMIN?
Managing Services the PowerShell way – Part 3: Start and stop Services
Over the last few articles we’ve been exploring different ways to gather relevant service information from local and remote computers using PowerShell. In Part 3 of this series I will explain how to start, stop and restart Services with PowerShell.
PS C:\> get-service bits Status Name DisplayName ------ ---- ----------- Running bits Background Intelligent Transfer Ser...
Since the command to get a service is called Get-Service, you can probably make a pretty good guess about the names of the other commands. Or, ask PowerShell to tell you all of the commands related to services.
get-command -noun service
Let’s look at these commands in a little more detail.
Stop-Service
To stop a service, all we need to do is specify the service name.
PS C:\> stop-service wuauserv
But you’ll get nothing written to the pipeline. Some cmdlets, like Stop-Service, are designed so that by default they do not write an object to the pipeline. But you can force it by using the –Passthru parameter.
PS C:\> stop-service bits -PassThru Status Name DisplayName ------ ---- ----------- Stopped bits Background Intelligent Transfer Ser...
If the service isn’t running the cmdlet won’t do anything, nor will you get an error. Sometimes the best thing is to pipe a service object to Stop-Service.
PS C:\> get-service browser | stop-service -WhatIf
What if: Performing operation “Stop-Service” on Target “Computer Browser (browser)”.
In this example I added the –WhatIf parameter which will indicate what the cmdlet would do if you allowed it. This is a nice sanity check. When I’m assured that I have the correct service, I can press the up arrow to get the previous command, delete –Whatif and stop the service.
PS C:\> get-service browser | stop-service
As I mentioned, if the service is already stopped, the cmdlet won’t really do anything. And even though there is no harm in running Stop-Service, if you prefer a more controlled approach you can use logic like this:
PS C:\> get-service bits | where {$_.status -eq 'running'} | stop-service -pass
Status Name DisplayName
—— —- ———–
Stopped bits Background Intelligent Transfer Ser…
If the service is running, then the service object is passed on in the pipeline and sent to Stop-Service. This is also a handy technique for stopping multiple services.
PS C:\> get-service bits,wsearch,winrm,spooler | where {$_.status -eq 'running'} | stop-service -whatif
What if: Performing operation “Stop-Service” on Target “Print Spooler (spooler)”.
What if: Performing operation “Stop-Service” on Target “Windows Remote Management (WS-Management) (winrm)”.
What if: Performing operation “Stop-Service” on Target “Windows Search (wsearch)”.
Some services may refuse to stop if there are dependent services as you can see in the screenshot.
stop-service
In these situations, the solution is to use the –Force parameter. This should work most of the time, but isn’t foolproof. You may have to try the command more than once. Be aware that this will also stop dependent services.
PS C:\> stop-service lanmanserver -force –PassThru Status Name DisplayName ------ ---- ----------- Stopped Browser Computer Browser Stopped lanmanserver Server
Start-Service
Starting a service works essentially the same way. It supports –Whatif and you’ll need to use –Passthru to see any objects.
PS C:\> start-service wuauserv -PassThru Status Name DisplayName ------ ---- ----------- Running wuauserv Windows Update
Again, if the service is already running the cmdlet has no impact. But you might try to start a service and get an error like in the screenshot below.
start-service
This is almost always due to a disabled service. In the next article I’ll explain how to configure service settings.
If you want to start a service and all of its dependencies, try a one line expression like this:
PS C:\> get-service lanmanserver | Foreach { start-service $_.name -passthru; start
-service $_.DependentServices -passthru}
Status Name DisplayName
------ ---- -----------
Running lanmanserver Server
Running Browser Computer Browser
We have to explicitly get all the dependent services because Start-Service won’t automatically start dependencies.
Restart-Service
Restarting a service also works the same way. Use –Passthru if you want to verify the service is running.
PS C:\> restart-service spooler -PassThru Status Name DisplayName ------ ---- ----------- Running spooler Print Spooler
Because this is stopping the service, you may need to use –Force.
Suspend and Resume
Some services can be suspended and resumed, in which case we can do so from PowerShell. But if the service doesn’t meet the requirements you’ll get an error like in the screenshot.
suspend-service
How can you tell? Look at the service object.
PS C:\> get-service bits | select *
Name : bits
RequiredServices : {RpcSs, EventSystem}
CanPauseAndContinue : False
CanShutdown : False
CanStop : True
DisplayName : Background Intelligent Transfer Service
DependentServices : {}
MachineName : .
ServiceName : bits
ServicesDependedOn : {RpcSs, EventSystem}
ServiceHandle : SafeServiceHandle
Status : Running
ServiceType : Win32ShareProcess
Site :
Container :
If the CanPauseAndContinue property is set to True then you should be able to suspend and resume.
PS C:\> get-service | where {$_.CanPauseandContinue}
Status Name DisplayName
------ ---- -----------
Running LanmanServer Server
Running LanmanWorkstation Workstation
Running MSSQLSERVER SQL Server (MSSQLSERVER)
Running O2FLASH O2FLASH
Running stisvc Windows Image Acquisition (WIA)
Running Winmgmt Windows Management Instrumentation
Not too many services meet this requirement.
PS C:\> suspend-service o2flash -PassThru Status Name DisplayName ------ ---- ----------- Paused O2FLASH o2flash
Then when I’m ready to resume:
PS C:\> resume-service o2flash -PassThru Status Name DisplayName ------ ---- ----------- Running O2FLASH o2flash
Both cmdlets also support –Whatif.
Remote Services
You may have noticed that all of my examples were done on the local machine. That wasn’t an accident. Unfortunately, even in PowerShell v3, none of these cmdlets have a parameter that allow you to manage a service on a remote computer. Yes, Get-Service supports –Computername but that’s it. You can see the service but you can’t do anything about it. Well, actually you can if the remote computer is running PowerShell v2 or later with remoting enabled. You can run all of the commands I’ve demonstrated in this article using Invoke-Command against a remote computer or PSSession. On the other hand, it also makes it very easy to manage the same service on multiple servers.
PS C:\> Invoke-Command {restart-service dns –passthru} –comp chi-dc03,chi-dc02,chi-dc01
invoke-command
There are additional ways to manage services on remote computers which I’ll cover in an upcoming article.
Summary
All of these cmdlets can be used in a pipeline expression and often that is the best option. Use Get-Service to retrieve service objects and then pipe them to the appropriate cmdlet. Be sure to look at complete help and examples for all of the cmdlets I’ve covered in this article. Next time we’ll look at configuring services.

By