PowerShell Basics #5: Manipulating services

Although you can start and stop the Windows services on your computer with the net.exe, PowerShell makes it far easier to do more complex manipulations.

Start by doing a Get-Service – or just casually called gsv.

PS D:\PowerShell> gsv

Status   Name               DisplayName
------   ----               -----------
Running  AEADIFilters       Andrea ADI Filters Service
Running  AeLookupSvc        Application Experience
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
Running  Appinfo            Application Information
Running  Apple Mobile De... Apple Mobile Device
Stopped  AppMgmt            Application Management
Stopped  aspnet_state       ASP.NET State Service

Again, notice you’re getting back a bunch of objects. These Service objects, of type System.ServiceProcess.ServiceController, can then be used to manipulate the actual services.

Note that manipulating services requires administrative permissions. If you get “Cannot open service…”-style errors, start your PowerShell window with the “Run as administrator” option.

Starting and stopping

The simple bits are starting and stopping services. There are two commandlets for this: Start-Service and Stop-Service, aliased as sasv and spsv. Also, to simplify the somewhat common action of restarting a service, there is a Restart-Service cmdlet available as well. These commandlets can take a parameter identifying the services to operate on, and they take either ServiceController objects or just strings that match service names.

For example, to stop all SQL Server related services, just do either of the following:

Get-Service *SQL* | Stop-Service
Stop-Service *SQL*

While the latter is simpler to write, the former is necessary with more complex filters. For example, suppose you wanted to start all the services that the Computer Browser service requires. To do this, you might type the following:

Get-Service Browser –Required | Restart-Service

This would then restart the Server and Workstation services.

If you need to use the suspended state, there are also Suspend-Service and Resume-Service commandlets available.

Reading and setting the start mode

Unfortunately, the ServiceController abstraction does not support reading the start mode of a service. If you need to check it out, you will want to use the WMI tools in PowerShell. That doesn’t really make things a lot more complex: the WMI objects representing services are much like ServiceControllers. For example, you could get some SQL services by doing this:

PS D:\PowerShell> Get-WMIObject Win32_Service | 
   where { $_.Name.StartsWith("SQL") } | 
   format-table Name, StartMode

Name                                                        StartMode
----                                                        ---------
SQLAgent$SQLEXPRESS                                         Disabled
SQLBrowser                                                  Disabled
SQLWriter                                                   Auto

Strangely enough, the Set-Service commandlet does support setting the start mode, even though Get-Service doesn’t return the value! So, you might go Set-Service SQL* –StartMode Disabled.

Doing it on remote computers

Get *-Service commandlets take a ComputerName parameter. To get the state of services on MyPC, just do gsv –c MyPC.

When you get back a list of service objects, you would expect to be able to pipe them over to Start-Service and friends. However, there’s a gotcha here: Only Get-Service and Set-Service have the ComputerName parameter. That also indicates the level of support: Only these two commandlets support remote use. If you do, say, gsv –c OtherPC AudioSrv | spsv, you end up stopping the Audio service on your computer, not on OtherPC.

In PowerShell v1, not even the ComputerName parameter exists. Therefore, all the Service commandlets are local-only. However, WMI can be used remotely, and it can manipulate services as well as list them.

In PowerShell v2, the ComputerName switch allows you to list and modify service settings on remote computers as well. Starting and stopping needs to happen through PowerShell remoting – a broader topic I will cover later on –, WMI or other methods (net.exe, sc.exe and so on).

April 29, 2010 · Jouni Heikniemi · No Comments
Tags:  · Posted in: .NET, Windows IT

Leave a Reply