Do Your Exchange Online Admin Tasks with PowerShell

By: Justin Braun
Posted on: January 26, 2016

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:

write-host "Getting credentials for connection to Exchange Online..."
$UserCredential = Get-Credential

write-host "Building session..."
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

write-host "Importing session and cmdlets..."
Import-PSSession $Session

write-host
write-host "Remember to close the session when complete using Remove-PSSession!"

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:

Get-PSSession | Remove-PSSession

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

exchangeps.txt

Related Articles

Compellent PSCS One-Liner Coding: Tip #2

With Compellent Storage Center 5, you have the ability to control whether an alert is generated for individual servers when connectivity changes.

Search Your Compellent Storage Center Using Windows PowerShell

Using Windows PowerShell and the Compellent Storage Center Command Set for PowerShell, I have create
Icsposh

PowerShell: Dynamic generation of an iCal/vCalendar (ICS) format file

Generating your own custom calendar or event invites in the iCal (vCalendar) format is pretty easy, once you've read the 140+ page spec.