Use Search-Mailbox to perform a email item count based on specific words for multiple mailboxes

Scenario:  You are asked the question if you can provide a count of email items based on certain words per mailbox.

You sure can! I will give you a CLUE

We are going to look in each mailbox (the $mbx variable) for any email items that contain each word (in the $word variable).

Specifically, since I think it was Colonel Mustard, I will add his name as a AND statement in the KQL Search.  Because this game can be complex, I  will also include a NEAR statement to add a little of that complexity to the search-mailbox searchquery-KQL language.


$mbx = “ProfessorPlum”, “MrsPeacock”, “MissScarlet”
$Word = “Candlestick”,  “Rope”,  “Wrench”
$word += “Ballroom NEAR(2) inside”, “Study NEAR(2) inside”, “Dining NEAR(2) Room”
$Result = @()

#Loop it
$mbx | %{
    $n = $_
    #Loop the Word
    $Word | %{
          $w = “$_”
          “Searching $n for word: $W”
          #Perform Serach
          $1 = (Search-Mailbox $n -SearchDumpster -SearchQuery “(Mustard) AND ($W)”  -EstimateResultOnly).ResultItemscount
          #Add to Table
          $returnobj = new-object psobject
          $returnobj |Add-Member -MemberType NoteProperty -Name “Name” -Value $n
          $returnobj |Add-Member -MemberType NoteProperty -Name “Word” -Value “(Mandava) AND ($W)” 
          $returnobj |Add-Member -MemberType NoteProperty -Name “EmailItemCount” -Value “$1” 
          $Result += $returnObj
          $w = $Null
          $1 = $null
    $n = $null

To view or Export:


$result | Export-csv C:tempresult.csv -notypeinformation


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: