PowerShell: How to Use Get-Service to Check Status of Service


You can use the Get-Service cmdlet in PowerShell to get all services on a computer, including running and stopped services.

Often you may want to use the Get-Service cmdlet to check the status of a particular service and to start a particular service if it is not currently running.

You can use the following methods to do so:

Method 1: Check Status of Specific Service

(Get-Service -Name ScDeviceEnum).Status

This particular example will return the status of the specific service named ScDeviceEnum.

For example, this may return “Running”, “Stopped”, “Paused”, etc.

Method 2: Start Specific Service if Not Currently Running

$ServiceName = 'ScDeviceEnum'
$ServiceDetails = Get-Service -Name $ServiceName

if ($ServiceDetails.Status -ne 'Running') {
Write-Host 'Service is Stopped'
Start-Service -Name $ServiceName -verbose
$ServiceDetails.Refresh()
Write-Host 'Service is' $ServiceDetails.Status
}
Write-Host 'Operation complete'

This particular example will attempt to start the service named ScDeviceEnum if it is not currently running.

The following example shows how to use these methods in practice.

Example: How to Use Get-Service to Check Status of Service

Suppose that we would like to check if the service named ScDeviceEnum is currently running on our computer.

We can use the following syntax to do so:

(Get-Service -Name ScDeviceEnum).Status

The following screenshot shows how to use this syntax in practice:

PowerShell Get-Service status

This returns Stopped, which tells us that this particular service is not currently running.

We can use the following script to attempt to start this service if it is not currently running:

$ServiceName = 'ScDeviceEnum'
$ServiceDetails = Get-Service -Name $ServiceName

if ($ServiceDetails.Status -ne 'Running') {
Write-Host 'Service is Stopped'
Start-Service -Name $ServiceName -verbose
$ServiceDetails.Refresh()
Write-Host 'Service is' $ServiceDetails.Status
}
Write-Host 'Operation complete'

If the service is currently stopped, then this script will return Service is Stopped.

It will then attempt to start the service and then output the results of whether or not we were able to successfully start the service.

If there is an error when attempting to start the service, the error message will appear.

Lastly, the script will output Operation complete to indicate that the script is finished.

Note: You can find the complete documentation for the Get-Service cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Use Get-Service and Filter Results
PowerShell: How to Filter for Unique Objects
PowerShell: How to Use Get-ChildItem with Filter
PowerShell: How to Use Get-ChildItem with Multiple Filters

Leave a Reply