Do Your Exchange Online Admin Tasks with PowerShell

Share on facebook
Share on twitter
Share on linkedin

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 -Credential $UserCredential -Authentication Basic -AllowRedirection

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

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):


More to explore...

#WindowsUglySweater is back!

It’s back again for the holiday season, and this year it shows-off everybody’s favorite 8-bit drawing app – MS Paint! The previous

Leave a Reply

Your email address will not be published.