0

Restart Exchange Services with PowerShell

If you’re hosting Microsoft Exchange server on-premises, you’ve probably realized that it is pretty critical to ensure those services are up and running. In the event that some services are not running, it’s always good to know how to restart Exchange services with Powershell.
&nsbp;

Knowing how to restart Exchange services can also be of use when you want to automate the starting of services when it goes down. The monitoring system you have in place should be able to detect that, and hopefully remediate it automatically so you’re not wasting cycles using manual intervention.

Requirements

In order to get started, we will certainly need to know a few things before hand. One of those things is obviously the name of the Server(s) that are running Exchange. Aside from that, having administrator rights on the box would be great.

Get Exchange Service Details

Before we touch base on restarting the services, we’ll want to know which services are going to be in scope so we’re not inadvertently restarting something that doesn’t need to be restarted. We have 2 options to get the information we need through Powershell. The first, we can use the Get-Service along with Invoke-Command to query the services. Second, we can use the Get-CimInstance along with the ComputerName parameter to query the machine for these services.
 

From a reporting standpoint, I personally like Get-CimInstance because it offers a bit more detail than the alternative. However, as far as actually restarting the services, we’ll stick to Get-Service and pipe it to Restart-Service to get the job done.
 

Get Exchange Service Details using Get-Service

As mentioned, we can use Get-Service to get the information we need. Let’s show a little snippet of how to do that.

Invoke-Command -ComputerName PAC-EXCH01 -ScriptBlock `
{Get-Service -Name *msexch* | select Name, DisplayName, StartType, Status}

Get-Exchange Services
 

Get Exchange Service Details using Get-CimInstance

Let’s cover the alternative for getting the exchange services using CimInstance.

Get-CimInstance -Class Win32_Service -ComputerName PAC-EXCH01 -Filter "Name like '%MSEXCH%'" | `
select Name, DisplayName, StartMode, StartName, State

Get Exchange Services CimInstance

 

Restart Exchange Services with PowerShell

Now that we know how to query the services, we can use Powershell to see which services are Exchange related as well as which ones are supposed to be running using Where-Object, a fundamental Powershell function.

Invoke-Command -ComputerName PAC-EXCH01 -ScriptBlock `
{Get-Service -Name *msexch* | Where-Object {$_.StartType -match 'Automatic' -and $_.Status -ne 'Running'} | select Name, DisplayName, StartType, Status}

Get-Exchange Services Not Running
 

From here, we can simply pipe the results to Restart-Service. Here’s how to do that.

Invoke-Command -ComputerName PAC-EXCH01 -ScriptBlock `
{Get-Service -Name *msexch* | Where-Object {$_.StartType -match 'Automatic' -and $_.Status -ne 'Running'} | Restart-Service }

Conclusion

Hopefully we were able to showcase how to restart Exchange services with PowerShell and allow you to either setup monitoring using PS, or to simply restart the service if it just happens to not be running.
 

If you liked this article, be sure to check out Get Exchange Schema Version Using Powershell. Also, be sure to take a look at our YouTube Channel for more sysadmin videos.

5/5 - (4 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

Leave a Reply

Your email address will not be published. Required fields are marked *