search mailbox office 365 powershell

"Search Mailbox in Office 365 with PowerShell"

By Anthony Harris
AD

In today's digital workspace, Microsoft Office 365 has become an indispensable tool for businesses and individuals alike. One of its powerful features is the ability to manage mailboxes using PowerShell, a task-based command-line shell and scripting language built on .NET. In this guide, we'll explore how to search mailboxes in Office 365 using PowerShell, making your email management tasks more efficient and accurate.

Understanding the Exchange Online PowerShell

The Exchange Online PowerShell is a remote shell for Exchange Online that allows you to perform tasks such as managing mailboxes, configuring policies, and more. It's the first step in our journey to search mailboxes in Office 365. Before we proceed, ensure you have the Exchange Online PowerShell module installed on your machine.

Connecting to Exchange Online PowerShell

To connect to Exchange Online PowerShell, you'll need to use the Connect-ExchangeOnline cmdlet. Here's how you can do it:

AD
Cannot add second Contact address for another domain Office 365 Shared mailbox
Cannot add second Contact address for another domain Office 365 Shared mailbox
9 Top Virtual Mailbox Providers
9 Top Virtual Mailbox Providers
I ran one PowerShell command and reclaimed space from a Windows folder nobody explains
I ran one PowerShell command and reclaimed space from a Windows folder nobody explains
Connect-ExchangeOnline -UserPrincipalName user@domain.com -ShowProgress $true

Replace user@domain.com with your Office 365 admin account. After a successful connection, you'll be prompted to enter your password.

Searching Mailboxes in Office 365

Now that we're connected, let's dive into searching mailboxes. The Search-Mailbox cmdlet is our tool of choice here. It allows us to search for items in a mailbox based on various criteria.

One PowerShell Command Completely Rebuilds Windows 11 Start Menu (When Nothing Else Works)
One PowerShell Command Completely Rebuilds Windows 11 Start Menu (When Nothing Else Works)
Outlook Is Microsoft's Answer To Everything You've Ever Hated About Hotmail
Outlook Is Microsoft's Answer To Everything You've Ever Hated About Hotmail
Microsoft 365 Uninstall Troubleshooter will help uninstall Office
Microsoft 365 Uninstall Troubleshooter will help uninstall Office

Basic Search

To perform a basic search, use the -SearchQuery parameter followed by the search term in quotes. Here's an example:

Search-Mailbox -Identity "User's Mailbox" -SearchQuery "search term"

Searching Specific Mailbox Folders

If you want to search within specific folders, you can use the -TargetMailbox and -TargetFolder parameters. For example, to search the Inbox folder:

AD
Search-Mailbox -Identity "User's Mailbox" -SearchQuery "search term" -TargetMailbox "User's Mailbox" -TargetFolder "Inbox"

Searching Multiple Mailboxes

To search multiple mailboxes, you can use the -Identity parameter with a comma-separated list of mailbox identities. Alternatively, you can use the Get-Mailbox cmdlet to retrieve a list of mailboxes and pipe it to the Search-Mailbox cmdlet.

the diagram shows how to use copilot across microsoft's web browsers
the diagram shows how to use copilot across microsoft's web browsers
How to use the new Outlook app on Windows 11
How to use the new Outlook app on Windows 11
Mailbox
Mailbox
Microsoft Dynamics 365 app for Outlook installation failed
Microsoft Dynamics 365 app for Outlook installation failed
How to Set Up an 'All Mail' Folder in Outlook
How to Set Up an 'All Mail' Folder in Outlook
a large wooden bookcase sitting on top of a white counter next to a window
a large wooden bookcase sitting on top of a white counter next to a window

Refining Your Search

PowerShell provides several parameters to refine your search. Here are a few:

  • -From and -To: Search within a specific date range.
  • -Subject: Search for a specific subject.
  • -Sender and -Recipient: Search for emails from or to specific users.

Exporting Search Results

After performing a search, you can export the results to a .csv file using the -ResultSize and -ResultSetSize parameters. Here's an example:

AD
Search-Mailbox -Identity "User's Mailbox" -SearchQuery "search term" -ResultSize Unlimited -ResultSetSize Unlimited | Format-Table -AutoSize | Out-GridView -Title "Search Results"

This will display the search results in a grid view, allowing you to sort and filter the data. You can then export the results to a .csv file.

What is USPS Form 1583 and Why Do You Need It For Your Virtual Mailbox?
What is USPS Form 1583 and Why Do You Need It For Your Virtual Mailbox?
17+ Stylish Block Mailbox Ideas to Inspire
17+ Stylish Block Mailbox Ideas to Inspire
Get ready to 'vibe work' in Microsoft Office with new AI agents β€” here's how
Get ready to 'vibe work' in Microsoft Office with new AI agents β€” here's how
a hand holding a cell phone next to a safe box with an appliance on it
a hand holding a cell phone next to a safe box with an appliance on it
a mailbox that is made out of wood and has the number 4797 on it
a mailbox that is made out of wood and has the number 4797 on it
26 Unique Mailbox Ideas Coastal & Nautical Style
26 Unique Mailbox Ideas Coastal & Nautical Style
5 Ways Virtual Addresses Protect Business Privacy | BusinessAnywhere
5 Ways Virtual Addresses Protect Business Privacy | BusinessAnywhere
πŸ“¬ Fed Up With Your Mailbox? Get a Virtual Mailbox Instead! Tired of... ❌ Racing home to check mail? ❌ Worrying about stolen packages? ❌ Piles of junk mail? ❌ Missing important letters while traveling? Fed Up Postal solves it all! βœ… Secure Digital Mailbox: Get a real US street address. βœ… Mail Scanning: See your mail online – we open & scan it (with your permission!). βœ… Package Handling: We receive, store, or forward your packages securely. βœ… Junk Mail Shredding: Auto-discard the clutter
πŸ“¬ Fed Up With Your Mailbox? Get a Virtual Mailbox Instead! Tired of... ❌ Racing home to check mail? ❌ Worrying about stolen packages? ❌ Piles of junk mail? ❌ Missing important letters while traveling? Fed Up Postal solves it all! βœ… Secure Digital Mailbox: Get a real US street address. βœ… Mail Scanning: See your mail online – we open & scan it (with your permission!). βœ… Package Handling: We receive, store, or forward your packages securely. βœ… Junk Mail Shredding: Auto-discard the clutter
Microsoft Outlook Keyboard Shortcuts: Printable Cheat Sheet
Microsoft Outlook Keyboard Shortcuts: Printable Cheat Sheet
a mailbox made out of wooden pallets with the number 4 lit up on it
a mailbox made out of wooden pallets with the number 4 lit up on it
a white and black box with numbers on it in front of some rocks, plants and trees
a white and black box with numbers on it in front of some rocks, plants and trees
Virtual office, virtual address, mail forwarding mailbox rental
Virtual office, virtual address, mail forwarding mailbox rental
a mailbox sitting in front of a set of steps with the number four on it
a mailbox sitting in front of a set of steps with the number four on it
It’s more than just a good-looking mailbox
It’s more than just a good-looking mailbox

Disconnecting from Exchange Online PowerShell

Once you're done, you can disconnect from Exchange Online PowerShell using the DisconnectExchangeOnline cmdlet:

DisconnectExchangeOnline

This will end your remote shell session with Exchange Online.

AD
AD