Check Windows Service Status – PowerShell Script

  1. Copy and save the below script as CheckMyService.ps1
  2. Open Powershell and navigate to the path where the script is saved
  3. Simply type part of the script name and then press tab for auto-complete
  4. You have to provide a service name as part of the script parameter

<#

Author: Khoa Nguyen

PS C:\Users\KoA\Dropbox\Code-Store\powershell> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  608

This is a quick script that can be ran as with a service name as a parameter to 
quickly check if a service is running or not or if it even exists.

You have to enter a parameter when calling the function, if not you will be
prompted for one in realtime.

Sample Executions:

PS C:\Users\KoA\Dropbox\Code-Store\powershell> .\CheckMyService.ps1 AGL
AGL not found
PS C:\Users\KoA\Dropbox\Code-Store\powershell> .\CheckMyService.ps1 SQLWriter
SQLWriter - Running
PS C:\Users\KoA\Dropbox\Code-Store\powershell> .\CheckMyService.ps1 fakeService
fakeService not found
PS C:\Users\KoA\Dropbox\Code-Store\powershell>

#>

param (
[Parameter(Mandatory=$true)]
[string] $ServiceName
)

if (Get-Service $ServiceName -ErrorAction SilentlyContinue)
	{
		if ((Get-Service -Name $ServiceName).Status -eq 'Running')
			{
				$ServiceStatus = (Get-Service -Name $ServiceName).Status
				Write-Host $ServiceName "-" $ServiceStatus
			}
		elseif ((Get-Service -Name $ServiceName).Status -eq 'Stopped')
			{
				$ServiceStatus = (Get-Service -Name $ServiceName).Status
				Write-Host $ServiceName "-" $ServiceStatus
			}	
		else
			{
				$ServiceStatus = (Get-Service -Name $ServiceName).Status
				Write-Host $ServiceName "-" $ServiceStatus
			}
	}
else
	{
		Write-Host "$ServiceName not found"
	}

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Eddie
Eddie
5 years ago

Quick questions.. 1.If I have more than one service that I need to check status, should I just add the new condition to the same if statement? Example: if (Get-Service $Print Spooler -and $WalletService -ErrorAction SilentlyContinue) ——————– 2. Does it work if I have spaces in the service name? Like this: param ( [Parameter(Mandatory=$true)] [string] $Print Spooler )   if (Get-Service $Print Spooler -ErrorAction SilentlyContinue) { if ((Get-Service -Name $Print Spooler).Status -eq ‘Running’) { $ServiceStatus = (Get-Service -Name $Print Spooler).Status Write-Host $Print Spooler “-” $ServiceStatus } elseif ((Get-Service -Name $Print Spooler).Status -eq ‘Stopped’) { $ServiceStatus = (Get-Service -Name $Print Spooler).Status… Read more »

Chandrakanth Reddy Lingareddy
Chandrakanth Reddy Lingareddy
5 years ago

How to export the output of the above script to the Excel or the Word file so that i can pick the file and i a can send a mail