$ErrorActionPreference = "SilentlyContinue" $OUtoProcess = "yourdomain.local/Some OU1/Some OU2/Users" $testrun = $true # If you do a test run a copy of what would be sent to your users is delivered to $testrunEmailto $smtpServer = "" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $emailFrom = "your.address@yourdomain.local" $testrunEmailTo = "your.address@yourdomain.local" $testrunBody = "" $a = get-Mailbox -ErrorAction $ErrorActionPreference -OrganizationalUnit $OUtoProcess | Where {$_.ForwardingAddress -ne $null} foreach ($item in $a) { $mailboxName = $item.name $mailboxAddress = $item.WindowsEmailAddress $body = "We are in the process of auditing access rights to shared mailboxes. According to our audit, you are getting forwarded mail which is being sent to another mailbox:" $body +="`n" $body +="`n" $body +=$mailboxName $body +="`n" $body +="`n" $smtpAddresses = get-mailbox $mailboxName | select -expand EmailAddresses | %{$_.SmtpAddress} $body += "This mailbox has the following email addresses:" $body +="`n" $body +="`n" foreach ( $addrout in $smtpAddresses ) { #don't list empty results or internal only email addresses if (($addrout -notlike "*.yourdomain.local") -and ($addrout -notlike "")) { $body +="`t" $body += $addrout $body +="`n" } } $subject = "Audit of mailbox $mailboxName ($mailboxAddress)" $emailTo = "" $emailTo = Get-Mailbox -Identity $item.ForwardingAddress | % {$_.PrimarySMTPAddress} $body += "`r`nPlease reply to this email with the following information: 1- If this mailbox is still needed or if this mailbox can be deleted. 2- If any of the email addresses associated with this mailbox are no longer used, and can be removed. Thanks, Your Name Your Title " # Collect all the e-mail body text from each email being sent if this is a test run, otherwise send the email if ($testrun) { $testrunBody += "`n`n*****" + "This would have been sent to: " + $emailTo + "*****`n`n" + $body } else { $smtp.Send($emailFrom, $emailTo, $subject, $body) } } # Send a single email if this is a test run if ($testrun) { $smtp.Send($emailFrom, $testrunEmailTo, "Fowarded email test run", $testrunBody) }