Exchange 2010 Unread Mail Count


How to get Exchange 2010 unread mail count from a mailbox’s inbox

You may be asked to find out how many unread emails are in a certain mailbox and there are a few scripts around that can do this via Outlook but that might not be terribly practical for an Exchange admin. The Microsoft Exchange Web Services Managed API allows us to do this through the Exchange 2010 Management Shell without the need to have Outlook installed. To get an Exchange 2010 unread mail count follow the below instructions:

You need the EWSUtil.dll saved to the appropriate location, you can get it here:

The below script will return a table with the unread email count:

$mbcombCollection = @()
$specificuser = get-mailbox -identity "username"
$mbcomb = "" | select DisplayName,EmailAddress,Unread
$mbcomb.DisplayName = $specificuser.DisplayName.ToString()
$mbcomb.EmailAddress = $specificuser.WindowsEmailAddress.ToString()
$mbMailboxEmail = $specificuser.WindowsEmailAddress.ToString()
"Mailbox : " + $mbMailboxEmail
$ewc = new-object EWSUtil.EWSConnection($mbMailboxEmail,$false, $null,$null,$null,$null)
$dTypeFld = new-object EWSUtil.EWS.DistinguishedFolderIdType
$dTypeFld.Id = [EWSUtil.EWS.DistinguishedFolderIdNameType]::inbox
$mbMailbox = new-object EWSUtil.EWS.EmailAddressType
$mbMailbox.EmailAddress = $mbMailboxEmail
$dTypeFld.Mailbox = $mbMailbox
$fldarry = new-object EWSUtil.EWS.BaseFolderIdType[] 1
$fldarry[0] = $dTypeFld
$fldList = $ewc.GetFolder($fldarry)
[EWSUtil.EWS.FolderType]$pfld = [EWSUtil.EWS.FolderType]$fldList[0];
$mbcomb.Unread = $pfld.UnreadCount
$mbcombCollection += $mbcomb

Copy that into a new notepad and save it as UnreadEmailCount.ps1 (or whatever you want to call it) then run it from Exchange 2010 Management Shell (don’t forget about execution policy).

How to get an email alert when a mailbox has unread emails

A scenario where this is useful is if you have an application that listens on mailboxes and imports the emails into its database. You may be required to setup automated email alerts to alert if this is failing. If a mailbox contains unread emails in the inbox it could warn you that your application isn’t working properly.

To have the above script send out an email alert replace the last line with the following:

$mbcombCollection > C:\Temp\UnreadEmailCheck.txt

IF ($mbcomb.Unread -ne "0") { Send-MailMessage -To "" -From "" -Subject "Unread Email Alert" -Body "Mailbox has unread emails please see attachment" -Attachment "C:\temp\UnreadEmailCheck.txt" -smtpserver smtpservername }

Now you just need to create a scheduled task to run the script on the required schedule. This script requires Exchange Management Tools so the scheduled task will call powershell.exe but will need the following argument:

-command ".'C:\Program Files\Microsoft\Exchange Server\V14\Bin\RemoteExchange.ps1'; Connect -ExchangeServer -auto; C:\Temp\UnreadEmailCount.ps1

Anytime the task runs and the mailbox has an unread email you will get an email alert.

Thanks for reading my article ‘Exchange 2010 Unread Mail Count’.

7 thoughts on “Exchange 2010 Unread Mail Count

  1. HI,
    when I run the script I get an Exception calling “.ctor” any ideas what is causing this, I have Full Access to the mailbox I am trying to query as well as Full Exchange Admin rights

    many thanks

    • Same here. The full error is “New-Object : Exception calling “.ctor” with “6” argument(s): “Exception of type ‘EWSUtil.ADSearchException’ was thrown.”

      Either there is a bug in the .dll provided or we are both missing some required component or other. If it helps, I am trying to run this against an Exchange 365 environment.

      • Oops, I also missed the message right before the errors: “No SCP found”. So it looks like it does have something to do with attempting to run this against a hosted Exchange environment, where a local lookup would of course fail. Unfortunately, I have no idea how to resolve this.

        • Hi, sorry should have replied to this ages ago but encase anyone is having similar issues…

          As you don’t have on premises Exchange this won’t work as the Autodiscover SCP won’t get created. Without the SCP the script can’t find EWS.

          If you have a hybrid deployment this could potentially work but I haven’t tried it.

    • Jamie Sevenoaks says:

      I am also getting the same results as Lefteris, this is exactly what I need but the count won’t work. Anyone got any suggestions?

      • I’ve not used this extensively, in fact I don’t think I’ve had cause to use this script since I first used it. As far as I know my initial implementation is still going strong so I don’t think any Exchange updates have broken it. But I do remember that it relied heavily on some Exchange configurations being correct:

        1. Autodiscover must be working, I also found that when using a reverse proxy the script tended not to work.
        2. EWS must be correctly configured, there is nothing unusual about EWS in my environment but if you have changed away from defaults for any reason it may affect this script.

        I suspect as the above two virtual directories are important that the SSL cert probably needs to be valid also i.e. not self-signed, expired, etc…

        Run the script manually using an elevated EMS session and check for any EWS or Autodiscover errors that occur and troubleshoot from there.

        You could also check if there are any updated versions of the EWSUtil DLL from MSFT. Encase there are newer versions it could be that it was updated to reflect changes from more recent Exchange 2010 roll ups, in which case update Exchange and use the latest DLL and see if you get the expected results.

        Thanks for reading.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>