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.

Jeffery HicksMVP By Jeffery Hicks - Tue, January 22, 2013 - 0 comments google+ icon

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with 20 years of experience. Follow his blog.

Articles in this series

PowerShell and Services

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

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.

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.

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.

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.

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:

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.

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

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.

Start-Service

Starting a service works essentially the same way. It supports –Whatif and you’ll need to use –Passthru to see any objects.

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

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:

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.

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

suspend-service

How can you tell? Look at the service object.

If the CanPauseAndContinue property is set to True then you should be able to suspend and resume.

Not too many services meet this requirement.

Then when I’m ready to resume:

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.

invoke-command

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.

Series NavigationManaging Services the PowerShell way – Part 2: Filtering - Managing Services the PowerShell way – Part 4: Configure Services

-1+1 - Rate this post
Loading ... Loading ...
Your question wasn't answered? Ask in the new 4sysops forum!
===Leave a Comment===

Login

Lost your password?