The Get-Service
command in PowerShell is the tool for listing, filtering, and managing the services running on a computer. This quick guide is aimed at helping you understand it’s capabilities, with examples that sysadmins will find especially useful.
Contents:
> Listing All Services on a Computer
> Filtering Services by Name
> Filtering Services by Status
> Starting, Stopping, and Managing Services
> Checking Service Dependencies
> Using Get-Service Remotely
> Exporting Service Info to CSV
Basic Usage of Get-Service
To get a list of all services on your computer, simply run the Get-Service
cmdlet:
# list all services on computer Get-Service
This will return a complete list of services, showing key details like the service name, display name, and status (Running, Stopped, etc.).
Filtering Services by Name
You can use wildcards (*
) to filter services by name. For example, to display all services with “SQL” in the name:
# list all sql services on computer Get-Service *sql*
This is particularly useful for identifying services related to a specific application or feature, such as all SQL
Filtering Services by Status
To narrow down the results further, you can filter services based on their status. For example, to list only “Stopped” services with “SQL” in the name:
# show all 'Stopped' SQL Services Get-Service *sql* | Where-Object {$_.Status -eq "Stopped"}
Common Service Status Values:
– Running
– Stopped
– Paused
– StartPending
– StopPending
Knowing these values can help you create specific filters to meet your needs.
Starting or Stopping a Service
DBAs & Sysadmins often need to start or stop services. You can use Start-Service
and Stop-Service
in combination with Get-Service
.
For example, to start all stopped SQL services run the following:
# start SQL Services if they are not running Get-Service *sql* | Where-Object {$_.Status -eq "Stopped"} | Start-Service
Or to stop all running SQL services:
# stop SQL agent service if its running # this is for demo purposes only Get-Service *sqlserveragent* | Where-Object {$_.Status -eq "Running"} | Stop-Service
Checking Service Dependencies
To investigate dependencies for a specific service, use the -DependentServices
or -RequiredServices
parameters:
# show dependent services for service example (Get-Service -Name MSSQLSERVER).DependentServices
This will list all services that depend on the specified service, helping you understand the impact of stopping or restarting it.
Using Get-Service Remotely
You can query services on a remote computer using the -ComputerName
parameter:
# Get services for remote computer Get-Service -ComputerName Server01
Combine this with filtering to check specific services on remote systems:
# check running sql services for remote computer Get-Service *sql* -ComputerName Server01 | Where-Object {$_.Status -eq "Running"}
Exporting Service Information to CSV
To save service details for reporting or documentation, you can export the results to a file:
# export service details to csv files Get-Service *sql* | Export-Csv -Path "C:\ServicesReport.csv" -NoTypeInformation
This generates a CSV file with the filtered service details, which can be easily shared or archived.
Leave a Reply