How to Stop All Instances of the Incoming E-Mail Service

This topic is going to discuss how to stop all instances of the "Microsoft SharePoint Foundation Incoming E-Mail" service in a SharePoint Server 2010 farm.   The sample script I'm providing does not require any parameters.  It simply queries the farm to discover all instances of this service, and then stops them.

Background

I was  recently setting another lab environment to run some repros for a customer environment.  Like most customer environments that I work with, I really wasn't planning on using the "Microsoft SharePoint Foundation Incoming E-Mail" service.  The problem is, this service is enabled by default when you join a server to a farm.  Because of this, I was looking for a quick way to disable all instances of this service on all servers in the farm, without having to manually go through SharePoint Central Administration and manually select each server and stop the service- time consuming in larger farms.

Approach

The approach I've taken is pretty simple, and likely  what most people familiar with PowerShell would have done.  As explained above, there are really only about 2 steps.  The first step is to query the farm for all instances of the "Microsoft SharePoint Foundation Incoming E-Mail" service.  The second step is to stop the service instance.

Solution

Step one was to retrieve a list of all service instances.  Luckily we have a PowerShell Cmdlet to do this.  The complete cmdlet we would use to perform this step looks something like this:

Get-SPServiceInstance | ? {$_.Typename -eq "Microsoft SharePoint Foundation Incoming E-Mail"}  

Step two was to stop all instances of the service instance.  We have a PowerShell Cmdlet to do this as well.  There are a few approaches to we could have taken when choosing to stop the service instances.  Had we assigned the output from the first cmdlet to a variable, we could use a foreach loop to iterate through the services and run the Stop-SPServiceInstance cmdlet against each object in the collection.  Since we're not doing anything overly complex here, we can basically just take the output of the first cmdlet and pipe it into the Stop-SPServiceInstance cmdlet directly.  That would look something like this:

Get-SPServiceInstance | ? {$_.Typename -eq "Microsoft SharePoint Foundation Incoming E-Mail"} | Stop-SPServiceInstance -Confirm:$False

You can download the Stop SharePoint Incoming Email Service script from this location: Stop SharePoint Incoming E-Mail Service Instances

Usage

Run the script within a PowerShell window.  Once the script completes all instances of the "Microsoft SharePoint Foundation Incoming E-Mail" service will be stopped.  This may take up to a minute or so for the immediate timer job to complete.  You can confirm the status of the service instance either through SharePoint central administration, which is again time consuming, or you could run the following PowerShell Cmdlet:
Get-SPServiceInstance | ? {$_.typename -eq "Microsoft SharePoint Foundation Incoming E-Mail"} |select server, status

Feedback

As always, if you have any questions or feedback, let me know. If you have any ideas to optimize the script, I'd like to hear that too. Thanks for reading!