Monitor Windows services with PowerShell

As a Windows Server and IIS administrator, you want your Windows services to run at all times. One can monitor Windows services in many, many, ways. Some of our customers websites may depend on certain services, which may be hard to monitor externally. For those Windows services that need local monitoring, I like to schedule a PowerShell script. Here is one…

How to monitor Windows Servers with PowerShell.

Start, stop, restart Windows services with PowerShell’s Get-Service and Start-Service cmdlets

To get the status of a service, we can utilize the Get-Service PowerShell cmdlet, and the Start-Service cmdlet to start a service.

Some of our clients depend on a mail service running at localhost. For example because they haven’t configured CDO cdoSendUsingPort in their classic ASP scripts. In the background, we use the IP Helper service to tunnel, or proxy, localhost:25 to an external SMTP service.

Bottom line, when the service isn’t running, some customers are unable to send mail from their websites.

In PowerShell, you can combine the output of the Get-Service cmdlet with Start-Service, to start a service when it’s not running:

Get-Service iphlpsvc | %{ if ( $_.Status -eq "Stopped" ) {
	Start-Service iphlpsvc
}}Code language: PowerShell (powershell)

If the status of the queried service is equal to “Stopped”, use Start-Service to start the service. You can use this for nearly every Windows service, for instance LanmanWorkstation (Workstation service):

Get-Service LanmanWorkstation | %{
	if( $_.Status -eq "Stopped" ) {
		Start-Service LanmanWorkstation
}Code language: PowerShell (powershell)

Or LanmanServer (Server service):

Get-Service LanmanServer | %{
	if( $_.Status -eq "Stopped" ) {
 		Start-Service LanmanServer
}Code language: PowerShell (powershell)

SQL Server and SQL Server Agent (is a dependent service):

PS C:\Users\jan> Get-Service MSSQLSERVER

Status   Name               DisplayName
------   ----               -----------

PS C:\Users\jan> Get-Service MSSQLSERVER -DependentServices

Status   Name               DisplayName
------   ----               -----------

PS C:\Users\jan> Stop-Service SQLSERVERAGENT
WARNING: Waiting for service 'SQL Server Agent (MSSQLSERVER) (SQLSERVERAGENT)' to stop...
PS C:\Users\jan> Get-Service SQLSERVERAGENT

Status   Name               DisplayName
------   ----               -----------

PS C:\Users\jan> Stop-Service MSSQLSERVER
WARNING: Waiting for service 'SQL Server (MSSQLSERVER) (MSSQLSERVER)' to stop...
[...]Code language: PowerShell (powershell)
Get-Service MSSQLSERVER | %{
	if( $_.Status -eq "Stopped" ) {
		Get-Service SQLSERVERAGENT | %{
			if ( $_.Status -eq "Stopped" ) {
				Start-Service SQLSERVERAGENT
		Start-Service MSSQLSERVER
}Code language: PowerShell (powershell)

Waiting for service state with Get-Service

If you are using Get-Service in PowerShell, you can even wait for a given time period before starting a service using the WaitForStatus method. For example:

$service = Get-Service( 'lanmanserver' )
$service.stop() # stops the given service
$service.WaitForStatus( 'Stopped', '0:01:00' ) # give the service a one minute time to fully stop
$service.start() # start the serviceCode language: PowerShell (powershell)

Test network connections in PowerShell with Test-NetConnection

You can use Test-NetConnection (PowerShell 4.0) or System.Net.Sockets.TcpClient to test network connections, and if they can be established:

(New-Object System.Net.Sockets.TcpClient).Connect( "localhost", "25" )
(New-Object System.Net.Sockets.TcpClient).Connect( "", "587" )

Test-NetConnection -Port 25 -ComputerName localhost
Test-NetConnection -Port 587 -ComputerName smtp.example.comCode language: PowerShell (powershell)

Interested in more monitoring using PowerShell? Have a look at some of my Zabbix posts where I use PowerShell and WMI / CIM to monitor websites and application pools in Windows Server IIS.

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at

Oldest Most Voted
Inline Feedbacks
View all comments
27/01/2020 22:34

Thank you for posting this. what about if I only need to be alerted when a service is stopped? I don’t want to auto stat it but need to be alerted when it stopped.

Reply to  Jan Reilink
29/01/2020 18:05

Thank you! I will give a try