As the support mechanism for many administrative tasks on Windows, managing and working with system services naturally fits into the administrator’s toolbox.
PowerShell offers a handful of cmdlets to help make working with system services easier: from listing services to lifecycle management and even to service installation.
To list all running services, use the Get-Service
cmdlet:
PS > Get-Service Status Name DisplayName ------ ---- ----------- Running ADAM_Test Test Stopped Alerter Alerter Running ALG Application Layer Gateway Service Stopped AppMgmt Application Management Stopped aspnet_state ASP.NET State Service Running AudioSrv Windows Audio Running BITS Background Intelligent Transfer Ser... Running Browser Computer Browser (...)
The Get-Service
cmdlet retrieves information about all services running on the
system. Because these are rich .NET objects (of the type
System.ServiceProcess.
ServiceController
), you can apply advanced filters
and operations to make managing services straightforward.
For example, to find all running services:
PS > Get-Service | Where-Object { $_.Status -eq "Running" } Status Name DisplayName ------ ---- ----------- Running ADAM_Test Test Running ALG Application Layer Gateway Service Running AudioSrv Windows Audio Running BITS Background Intelligent Transfer Ser... Running Browser Computer Browser Running COMSysApp COM+ System Application Running CryptSvc Cryptographic Services
Or, to sort services by the number of services that depend on them:
PS > Get-Service | Sort-Object -Descending { $_.DependentServices.Count } Status Name DisplayName ------ ---- ----------- Running RpcSs Remote Procedure Call (RPC) Running PlugPlay Plug and Play Running lanmanworkstation Workstation Running SSDPSRV SSDP Discovery Service Running TapiSrv Telephony (...)
Since PowerShell returns full-fidelity .NET objects that
represent system services, these tasks and more become incredibly
simple due to the rich amount of information that PowerShell
returns for each service. For more information about the
Get-Service
cmdlet, type
Get-Help
Get-Service
. For more information about filtering,
grouping, and sorting in PowerShell commands, see Filter Items in a List
or Command Output.
Note
The Get-Service
cmdlet displays
most (but not all) information about running services. For additional
information (such as the service’s startup mode), use the
Get-CimInstance
cmdlet:
$service = Get-CimInstance Win32_Service | Where-Object { $_.Name -eq "AudioSrv" } $service.StartMode
In addition
to supporting services on the local machine, the Get-Service
cmdlet lets you retrieve and manage
services on a remote machine as well:
PS > Get-Service -Computer <Computer>
|
Sort-Object -Descending { $_.DependentServices.Count }
Status Name DisplayName
------ ---- -----------
Running RpcEptMapper RPC Endpoint Mapper
Running DcomLaunch DCOM Server Process Launcher
Running RpcSs Remote Procedure Call (RPC)
Running PlugPlay Plug and Play
Running nsi Network Store Interface Service
Running SamSs Security Accounts Manager
(...)
For more information about working with classes from the .NET
Framework, see Work with .NET Objects. For more
information about working with the Get-CimInstance
cmdlet, see Chapter 28.
To stop a service, use the Stop-Service
cmdlet:
PS > Stop-Service AudioSrv -WhatIf What if: Performing operation "Stop-Service" on Target "Windows Audio (AudioSrv)".
Likewise, use the Suspend-Service
, Restart-Service
, and Resume-Service
cmdlets to suspend, restart, and
resume services, respectively.
The Stop-Service
cmdlet lets you stop a service either
by name or display name.
Note
Notice that the Solution uses the -WhatIf
flag on the Stop-Service
cmdlet. This parameter lets you see
what would happen if you were to run the command but doesn’t
actually perform the action.
For more information about the Stop-Service
cmdlet, type Get-Help Stop-Service
. If you
want to suspend, restart, or resume a service, see the help for the
Suspend-
Service
, Restart-Service
, and Resume-Service
cmdlets, respectively.
To configure a service (for example, its description or startup
type), see Configure a Service. In addition to
letting you configure a service, the Set-Service
cmdlet described in that recipe also
lets you stop a service on a remote computer.
To configure a service, use the Set-Service
cmdlet:
PS > Set-Service WinRM -DisplayName 'Windows Remote Management (WS-Management)' ` -StartupType Manual
The Set-Service
cmdlet lets you manage the
configuration of a service: its name, display name, description,
and startup type.
If you change the startup type of a service, your natural next step is to verify that the changes were applied correctly. List All Running Services shows how to view the properties of a service, including the WMI-based workaround to examine the startup type.
In addition
to letting you configure services on the local computer, the
Set-Service
cmdlet also offers the
-ComputerName
parameter to configure
services on remote computers.