cancel
Showing results for 
Search instead for 
Did you mean: 
Create Post

Working with Office 365 via PowerShell

MVP

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 https://ps.outlook.com/powershell/ -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

2017-11-29_09-59-28.png
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

12 Comments

OK.  I'm impressed.

Level 12

mrxinu,

Nice job on this Geek Speak for O365 Exchange Online. Looking forward to more items from you on the Cloud Based Applications and Services we have lost visibility to when moved from the on premise to the Cloud.

Best Regards,

Derik Pfeffer

Loop1 Systems: SolarWinds Training and Professional Services

The folks in our IS Security department tell me they already knew all about this, and leverage it on a daily basis.

My thought:  "Wouldn't it have been nice if the rest of us knew about this, and had some training so we can help be part of the solution, too?"

MVP
MVP

Thank you sir. Also, yeah, it's a secret. Can't be sharing the information.

MVP
MVP

You're too kind my friend.

Level 14

Very nice... you have no idea how timely this was....

Level 15

Thanks! Saved a bunch of time

Here's my problem:

We have too many chefs in the kitchen and we are trying to setup an active monitoring scenario for whenever someone executes a content search in email. We want to know within 5 minutes when an O365 admin kicks one off.

This one has been a head-scratcher for us.

Level 13

Nice, thank you for the example.

This was really useful.

One of the best things I also found was the extra information you can gather from logging into the Azure AD portal that has been setup for you as part of Office 365 but Microsoft casually forgets to tell you about.

MVP
MVP

Nice

Level 21

We provide managed services using Azure and what we have learned from both experience and in working with Microsoft is that Azure is pretty much designed to be managed using PowerShell on the back end.  There are a lot of tasks that you can do using PowerShell that are not available at all using the GUI.

About the Author
Started out as a Unix sysadmin, writing backup scripts for old NCR towers that wrote to 3M tapes. I can still remember the options I used for cpio, dd, and find. From there I worked in a NOC doing frame relay and ATM testing, rounding out with point-to-point T1s. After that I spent a few years taking care of some routers, switches and a handful of Linux servers for HP running their special projects. Leaving there I started my stint as a road warrior, traveling all over the country (and sometimes out of it) doing instruction, configuration and customization of SolarWinds installations for customers ranging from retail to government. Now I work from my home in the Pacific Northwest as Loop1's integrator blending systems together with PowerShell, Python, C#, SQL, SWQL, and any other glue necessary to do the job. Tags: automath, punctual, detail-oriented, careful, gregarious introvert, puzzle solver, solution finder, always learning