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

Be More Fluid with the New Storage Center Command Set for PowerShell

Windows PowerShell was designed as a scripting language to help IT administrators automate repetitiv
665

PowerShell One-Liner: SC Series storage class and RAID level allocations

With the PowerShell SDK for Dell EMC SC Series arrays, you can easily retrieve volume information including allocated, free, and used space.

The Case of the Missing Interview

Even though we had a number of technical difficulties, I really enjoyed the interview with Jon and Hal over at the Powerscripting Podcast.