Do Your Exchange Online Admin Tasks with PowerShell

There are some things in Exchange that you just need to use PowerShell for.  If you use Exchange Online or Office 365, the web portal exposes a lot of the admin functionality that you might need, but there are certain actions that require PowerShell.

Accessing the Exchange PowerShell cmdlets on a local server is one thing, but accessing those cmdlets in a hosted environment can be a little trickier.  After I had to type these a half-dozen times, my brain wasn’t getting it quite yet, so I build a little script that covers what you need to need to establish a connection to the remote Exchange environment.

Here’s what the script does:

  1. Get your admin user credentials for Exchange Online
  2. Make the connection to Office 365/Exchange Online and build a session
  3. Import the session which loads the Exchange module and makes it’s cmdlets available for execution

Here’s what the code looks like:

Once you are done with your work, make sure that you close down and disconnect the session by Remove-PSSession cmdlet.  If you instantiate too many sessions, you’ll hit a limit and will have to wait for those sessions to expire.  To close the session, you need to pass in the variable of your session to the Remove-PSSession cmdlet.

For the easy button, you can whack all your sessions with this code:

If you’re not into cut and paste, you can download the script here (you’ll have to rename it to a .ps1 file):