Chapter 25. System Services

Introduction

As the support mechanism for many administrative tasks on Windows, managing and working with system services naturally fits into the administrator's toolbox.

1 comment

  1. Richard Siddaway Posted 9 days and 22 hours ago

    Reviewing

Add a comment

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.

1 comment

  1. Aleksandar Nikolic Posted 16 days and 7 hours ago

    from listing services, to lifecycle management --> from listing services to lifecycle management

Add a comment

List All Running Services

Problem

You want to see which services are running on the system.

Solution

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
(...)

Discussion

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 the section called “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-WmiObject cmdlet:

$service = Get-WmiObject 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
(...)

1 comment

  1. Aleksandar Nikolic Posted 16 days and 7 hours ago

    -Computer --> -ComputerName

Add a comment

For more information about working with classes from the .NET Framework, see the section called “Work with .NET Objects”. For more information about working with the Get-WmiObject cmdlet, see Chapter 28, Windows Management Instrumentation.

Manage a Running Service

Problem

You want to manage a running service.

Solution

To stop a service, use the Stop-Service cmdlet:

PS > Stop-Service AudioSrv -WhatIf
What if: Performing operation "Stop-Service" on Target "Windows Audio (Audi
oSrv)".

Likewise, use the Suspend-Service, Restart-Service, and Resume-Service cmdlets to suspend, restart, and resume services, respectively.

1 comment

  1. Aleksandar Nikolic Posted 16 days and 7 hours ago

    The same statement has been repeated a few sentences later. I would suggest removing this one.

Add a comment

Discussion

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.

1 comment

  1. Richard Siddaway Posted 9 days and 22 hours ago

    mention that these cmdlets don't work on remote computers ie no -computername parameter but that can achieve simile effects using Set-Service

Add a comment

To configure a service (for example: its description or startup type), see the section called “Configure a Service”. In addition to letting you configure a service, the Set-Service cmdlet it describes also lets you stop a service on a remote computer.

Configure a Service

Problem

You want to configure properties or startup behavior of a service.

Solution

To configure a service, use the Set-Service cmdlet:

PS > Set-Service WinRM -DisplayName 'Windows Remote Management (WS-Management)' `
    -StartupType Manual

Discussion

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. the section called “List All Running Services” shows how to view the properties of a service, including the WMI-based workaround to examine the startup type.

2 comments

  1. Aleksandar Nikolic Posted 16 days and 7 hours ago

    the section --> The section

  2. Lee Holmes Posted 14 days and 17 hours ago

    Thanks. That looks like a bug in the feedback system - these are automatically generated hyperlinks.

Add a comment

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.

You must sign in or register before commenting
*
*
*
*
*

Atom Icon Comments on this page or Comments on the whole book.