User in a panic, “OMG! I need you to delete the email I accidentally sent to the wrong people.” Tech in a calm cool voice, “Who’d you send it to?”. Inevitably this conversation leads to you recieving a list of addresses that the user copy and pastes out of the To, CC, or BCC field of an Outlook object.

Unfortunatley, this data will not be in a useable format as far as bulk PowerShell operations are concerned. The Outlook name resolution feature will have changed the user’s names or email addresses to the format “First, Last <>;” The extra characters make using the data as the identity pararmeter in the Export-Mailbox or Search-Mailbox cmdlets impossible . Many people end up editing the list in Excel or Notepad to remove the extra text and get down to a list of just email addresses.

$arr = @()
$path = "$env:USERPROFILE\documents\outlooklist.txt"
$pattern = "(?)
$list = Get-Content $path
$list -split ';'|Foreach {if ([Regex]::IsMatch($_, $pattern)) {
           $arr += [Regex]::Match($_, $pattern)
$arr | Foreach {$_.Value}|out-file -FilePath $env:USERPROFILE\documents\emailaddresses.txt

This PowerShell code uses RegEx pattern matching to turn your mess of a list into a useable list of email addresses. The split adds a carrige return after each semicolon and the RegEx pattern matches everthing between the < and > symbols. Incidentally, I searched all over the internet trying to find somebody else that was correctley using RegEx to copy the text between two symbols and never found anyone that did it correctly, so I’m pretty proud of myself for figuring this out.

You’ll need to paste your email dump into a text file and save it somewhere. Then alter the $path variable to point at it. When you execute the script it will output the email addresses to a file in your documents folder named emailaddresses.txt, ┬ábut you could easily pipe it into your Search-Mailbox cmdlet instead.