WordPress Websites for Engineers: Elevate Your Brand
How To Get Services Status - PowerShell
1. How To Get Services Status - PowerShell
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
PowerShell Script - Get Service Status..........................................................................................................1
Code Snippet.............................................................................................................................................1
PowerShell Output....................................................................................................................................2
2. How To Get Services Status - PowerShell
1 | P a g e
Overview
On Windows platform, managing services with windows PowerShell is easier than earlier. PowerShell
provides cmdlet to manage services for both local and remote servers. In order to manage services you
need to utilize “Get-Service” cmdlet.
In this post / snippet we will demonstrate, listing the services on localhost and listing them according to
service state that are stopped and running on the server.
Applies To
Tested on Windows 10, Windows 2008 R2, Windows 2012.
Pre-Requisites
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
PowerShell Script - Get Service Status
This script will list all the services configured on the server along with current status.
Code Snippet
#
# Get Service Status sorted by Status and Name
#
$AllServices = Get-Service | Sort-Object Status, Name
Clear-Host
foreach ($SVC in $AllServices)
{
if ( $SVC.status -eq "Stopped")
{
Write-Host $SVC.name, "`t" $SVC.status -ForegroundColor Red
}
else
{
Write-Host $SVC.name, "`t" $SVC.status -ForegroundColor Green
3. How To Get Services Status - PowerShell
2 | P a g e
}
}
PowerShell Output
In this example we will restrict the output to only first 10 and excluding status in get-service as below
snippet;
$AllServices = Get-Service | Sort-Object Name | select -First 10