Working with Office 365 via PowerShell

So you've taken the leap, battled through the migration, and your mail is in the Microsoft cloud. You turned off your locally hosted and managed Exchange cluster and reclaimed those hours of sleep lost to responding to alerts about how so-and-so can't send an email at 2:00 am on a Saturday morning.

You probably are also missing the ease of monitoring that your on-prem solution provided. Now, with the details at arm's length, your usual monitoring methods don't work anymore. Don't worry, Microsoft has you covered, if and only if you've been keeping your PowerShell skills sharp.

Introducing: Remote PowerShell Sessions for Microsoft Office 365

Here's how it works:

Connect to Office 365

Create a new PowerShell session, and import it into your current session. You’ll be prompted for credentials when this portion of code runs.

Important: Your Office 365 credentials are in the UPN format username@domain, not domain\username. Chasing authentication issues only to realize I was not using the correct format definitely caused me some grief.

$office365 = New-PSSession -ConfigurationName "Microsoft.Exchange" -ConnectionUri -Credential (Get-Credential) -Authentication Basic -AllowRedirection

Import-PSSession $office365 | Out-Null

Query for the Information

At this point, you have access to the same commands that you would have had were you using the Exchange PowerShell module locally. Here you will find numerous sources of valuable monitoring data.  For example, to get the details on all of the inactive mailboxes:

$inactiveMailboxes = Get-Mailbox -InactiveMailboxOnly

View Your Results

At this point, you have an array of your inactive mailboxes, but what information do you have at your fingertips? An easy to way to find out is by making use of PowerShell’s Out-GridView cmdlet to give you an easy-to-use (and filter!) interface to pore over the data:

$inactiveMailboxes | Out-GridView

Do Something with Them

Now that you have your inactive mailboxes, what should you do with them? You have a couple of options, which are described in the following article:

Delete or restore user mailboxes in Exchange Online

Definitely read through the information in this article to help ensure that you don’t do something you’ll regret later, such as permanently delete a mailbox you want back. Assuming you’re ready to part with your inactive mailboxes, it’s as easy as this:

$inactiveMailboxes | Remove-Mailbox -Confirm: $false

By leveraging the clout of the PowerShell pipe, you can send your mailbox objects directly from your $inactiveMailboxes array right into the Remove-Mailbox cmdlet.

Next Up: Working with Azure Active Directory via PowerShell

THWACK - Symbolize TM, R, and C