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"
	}

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Eddie Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Eddie
Guest
Eddie

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
Write-Host $ServiceName “-” $ServiceStatus
}
else
{
$ServiceStatus = (Get-Service -Name $Print Spooler).Status
Write-Host $Print Spooler “-” $ServiceStatus
}
}
else
{
Write-Host “$Print Spooler not found”
}

———————

Thank you so much!

Best regards,
Eddie