Finding Exchange Servers Using Active Directory and Powershell

Sometimes PowerShell scripts need to send email messages to other users, and it is pretty annoying to hard code SMTP servers if you’ve got a ton of scripts lying around across several different servers. Wouldn’t it be nice if the script could find a valid Exchange server from AD? Well, not only is this possible, it’s pretty easy as well:

The above Gist is just a simple example of that. If I wanted to find all hub transport servers, I can filter it quickly:

Get-ExchangeServers | ? { $_.Roles -contains "HT" }

Now, you’ll never need to hard code servers in your scripts again!