Search code examples
.netemailpowershellexchange-server

How to check an exchange mailbox via powershell?


How would I go about using powershell to return the text and headers of the last 5 messages received to my exchange email account? Is there a simple way/library to do this?

This is related to my question about not using outlook on superuser. Except that having not found any good alternatives I figure I might as well write my own simple powershell client.


Solution

  • You'll need to have the EWS API installed, and you'll need to check the path to the DLL in the Reflection Assembly load portion.

    This should get you to the point where you're able to work with the $inbox.FindItems(5) statement and filter the results you want out of that.

    [Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll")
    $s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
    $s.Credentials = New-Object Net.NetworkCredential('user', 'pass', 'domain')
    $s.AutodiscoverUrl("email@address.com")
    
    $inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
    $inbox.FindItems(5)