PowerShell Command Get-Service

Get-Service | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Service.

When to use Get-Service?

The Get-Service cmdlet gets objects that represent the services on a computer, including running and stopped services. By default, when Get-Service is run without parameters, all the local computer’s services are returned.

You can direct this cmdlet to get only particular services by specifying the service name or the display name of the services, or you can pipe service objects to this cmdlet.

What version of PowerShell am I using?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-Service?

Get all services on the computer:

Get-Service

This example gets all of the services on the computer. It behaves as though you typed Get-Service *.

The default display shows the status, service name, and display name of each service.

Get services that begin with a search string:

This example retrieves services with service names that begin with win.

Get-Service “win*”

Get services that begin with a search string and an exclusion:

This example only gets the services with service names that begin with win, except for the WinRM service.

Get-Service -Name “win*” -Exclude “WinRM”

Display services that are currently active:

Get-Service gets all the services on the computer and sends the objects down the pipeline.

The Where-Object cmdlet, selects only the services with a Status property that equals Running.

Status is only one property of service objects. To see all of the properties, type Get-Service | Get-Member

Get-Service | Where-Object {$_.Status -eq “Running”}

List the services on the computer that have dependent services:

The Get-Service cmdlet gets all the services on the computer and sends the objects down the pipeline. The Where-Object cmdlet selects the services whose DependentServices property is not null.

The results are sent down the pipeline to the Format-List cmdlet. The Property parameter displays the name of the service, the name of the dependent services, and a calculated property that displays the number of dependent services for each service.

Get-Service |
 Where-Object {$_.DependentServices} |
   Format-List -Property Name, DependentServices, @{
    Label=”NoOfDependentServices”; Expression=.{$_.dependentservices.count}
}

Sort services by property value:

This example shows that when you sort services in ascending order by the value of their Status property, stopped services appear before running services.

The reason is because the value of Status is an enumeration, in which Stopped has a value of 1, and Running has a value of 4.

To list running services first, use the –Descending parameter of the Sort-Object cmdlet.

Get-Service “s*” | Sort-Object status

Get the dependent services of a service:

This example gets the services that the WinRM service requires. The value of the service’s ServicesDependedOn property is returned.

Get-Service “WinRM” -RequiredServices

Learn last week’s command: Get-PSDrive.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.