Skip to content
The Lazy Administrator
  • Home
  • Disclaimer
  • Contact
  • About Me
  • Search Icon

The Lazy Administrator

Finding ways to do the most work with the least effort possible

Create Bulk Office 365 Compliance Searches with PowerShell

Create Bulk Office 365 Compliance Searches with PowerShell

March 18, 2018 Brad Wyatt Comments 0 Comment

Recently I wanted to find a way to get PowerShell to create compliance searches that followed keyword queries and search conditions. This means I could have multiple values in one search query. For example, “TO brad wyatt AND FROM [email protected]”. This query would search for e-mails sent to Brad which only from [email protected]. To get PowerShell to do this I decided to create a Hash Table that would have the queries listed in the table.

After it would create each compliance search it would then wait for the search to gather all of the results and then export the amount of items it found to a csv file. Each Compliance Search would append its results to the same file so in the end I would have a CSV file containing all of the Compliance Searches and the objects it found for each one.

To keep from having the Compliance Search having names with invalid characters I made it so the name of the Compliance Search would start with “Case 1” and increment by 1 for each new search.

 

The results CSV will show you the case name, the query and the total amount of items that were found that matched the query.

With each Compliance Search I set the following parameters to True: AllowNotFoundExchangeLocationsEnabled and I set the following parameters to All: ExchangeLocation

The AllowNotFoundExchangeLocationsEnabled parameter specifies whether to allow inactive mailboxes in the compliance search. An inactive mailbox is a mailbox that’s placed on Litigation Hold or In-Place Hold before it’s soft-deleted. Valid values are:

  • $true   The search doesn’t try to validate the existence of the mailbox before proceeding. This value is required if you want to include inactive mailboxes in the search, because inactive mailboxes don’t resolve as regular mailboxes.
  • $false   The search tries to validate the existence of the mailbox before proceeding. If you specify an inactive mailbox or a mailbox that otherwise can’t be found, the search will fail. This is the default value.

The ExchangeLocation parameter specifies the mailboxes to include. Valid values are:

  • A mailbox
  • A distribution group or mail-enabled security group (all mailboxes that are currently members of the group).
  • The value All for all mailboxes. You can only use this value by itself.

 

Script

$ResultsCSV = "C:\ComplianceCase_Results.csv"

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

$Terms = @("Brad Wyatt",
	'"Microsoft" AND "Sales"',
	'"Microsoft" AND "Commissions"',
	'"Microsoft" AND "Orders"',
	'"Microsoft" AND "Shipments"',
	'"MSFT" AND "Sales"',
	'"Mcdonalds" AND "resume"',
	'"Apple" AND "schematics"',
	'"Microsoft" AND "infringement"',
	'"Apple" AND "leaving!"',
	'"Reddit" AND "sample!"',
	"store",
	"Fresh sales today",
	"group admin",
	"groupadmin",
	'"bwyatt" AND "Sale order commission shipment" AND "Microsoft"',
	'"Brad Wyatt" AND "Sale order commission shipment" AND "Microsoft"',
	'To: "[email protected]" AND From:  "[email protected]"',
	'To: "[email protected]" AND From:  "HR@microsoftcom"',
	'From: "[email protected]" AND To:  "[email protected]"',
	'From: "[email protected]" AND To:  "[email protected]"',
	'From: "Brad" AND To: "[email protected]"',
	'From: "Admin" AND To: "[email protected]"',
	'To: "John" AND From: "[email protected]"',
	'Subect:"poaching brad"',
	"Letter of resignation",
	"PowerShell",
        ' (sent=01-01-2011..12-31-2018) AND (CC: "Brad Wyatt") ',
        ' (received=01-01-2011..12-31-2018) AND (From: "Brad Wyatt") ',
        ' (sent=01-01-2011..12-31-2018) AND (From: "Brad Wyatt") ',
        ' (sent=01-01-2011..12-31-2018) AND (Bcc: "Brad Wyatt") '
)


$Count = 1

Foreach ($Term in $Terms)
{
	Write-Host "Creating case to search for $Term.." -ForegroundColor Yellow
	$CaseName = "Case" + $count++
	Write-Host "Case Name: $CaseName"
	New-ComplianceSearch -Name $CaseName -ExchangeLocation All -AllowNotFoundExchangeLocationsEnabled $true -ContentMatchQuery $Term
	Write-Host "Starting case.. $CaseName"
	Start-ComplianceSearch $CaseName
	Start-Sleep -Seconds 5
	Do
	{
		Write-Host "Waiting until $CaseName is finished running..."
		$Status = Get-ComplianceSearch $CaseName | Select-Object -ExpandProperty Status
	}
	Until ($Status -eq "Completed")
	Write-Host "Gathering item count for $CaseName..."
	Get-ComplianceSearch $CaseName | Select-Object Name, ContentMatchQuery, Items | Export-Csv -NoTypeInformation $ResultsCSV -Append
}



 

 

Brad Wyatt
Brad Wyatt

My name is Bradley Wyatt; I am a 5x Microsoft Most Valuable Professional (MVP) in Microsoft Azure and Microsoft 365. I have given talks at many different conferences, user groups, and companies throughout the United States, ranging from PowerShell to DevOps Security best practices, and I am the 2022 North American Outstanding Contribution to the Microsoft Community winner.


Office 365, PowerShell
Compliance, Compliance Center, Exchange, PowerShell, Search

Post navigation

PREVIOUS
Monitor Share and NTFS Permissions and E-Mail Changes
NEXT
Get Friendly License Name for all Users in Office 365 Using PowerShell

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe

Email


Categories

  • Active Directory (8)
  • AI (3)
  • API (1)
  • AutoPilot (2)
  • Azure (15)
  • Bicep (4)
  • Connectwise (1)
  • Defender for Cloud Apps (1)
  • Delegated Admin (1)
  • DevOps (6)
  • Graph (6)
  • Intune (15)
  • LabTech (1)
  • Microsoft Teams (6)
  • Office 365 (19)
  • Permissions (2)
  • PowerShell (50)
  • Security (1)
  • SharePoint (3)
  • Skype for Business (1)
  • Terraform (1)
  • Uncategorized (2)
  • Yammer (1)

Recent Comments

  • Side Eye on Homeland Security’s Trusted Travelers API and PowerShell – Getting a Better Global Entry Interview Using PowerShell
  • Lisa on Allow Non-Admin Users to Manage Their Desktop Icons Using Intune
  • A1 Lottery LOGIN on Get a New Computer’s Auto Pilot Hash Without Going Through the Out of Box Experience (OOBE)
  • Kristopher Gates on Getting Started with GitHub Copilot in the CLI
  • MD SHARIQUE AKHTAR on Modern Active Directory – An update to PSHTML-AD-Report

1,746,499 People Reached

© 2025   All Rights Reserved.