Taking on PowerShell one cmdlet at a time

Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will walk through each PowerShell command every week, explaining when and how to use them. Adam will be covering Get-Service this week.

When should you use Get-Service
The Get-Service cmdlet returns objects that represent services on a computer. This includes running and stopped services. All services on the local computer are returned by default when Get-Service runs without parameters.
This cmdlet can be used to only get certain services. You can specify the service name or display name of the services or pipe service objects to it.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.

How do I use Get-Service
All services available on the computer
This example will get all the services available on the computer. It behaves like you typed Get Service *.
The default display shows the status, name of service, and display name for each service.

Services that start with a search string
This example retrieves services whose service names begin with win.
Get-Service “win*”

Services that start with a search string followed by an exclusion
This example will only include services whose service names begin with win, except the WinRM service.
Get-Service -Name “win*” -Exclude “WinRM”

Display services currently in use:
Get-Service retrieves all services from the computer and sends them down the pipeline.
The Where-Object cmdlet selects only services that have a Status property equal to Running.
Status is only one property for service objects. Get-Member
Get-Service | Where-Object $_.Status -eq “Running”

List the dependent services that are available on your computer
The Get-Service cmdlet retrieves all services on the computer and sends them down the pipeline. The Where-Object cmdlet allows you to select services whose DependentServices property does not null.
The results are sent to the Format-List cmdlet via the pipeline. The Property parameter displays the service’s name, the name and the dependent services. A calculated property displays the number of dependent service 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 if you sort services by their Status property in ascending order, stopped services will appear before running services.
This is because Status is an enumeration. Stopped has a value 1 and Running has a valued 4.
You can list running services first by using the -Descending parameter in the Sort-Object cmdlet.
Sort-Object status

Get the dependent services of a Service:
This example provides the WinRM service with the required services. The ServicesDependedOn property value is returned.
Get-Service “WinRM” -RequiredServices

Get-PSDrive – Last week’s command
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.