Author Posts

July 19, 2018 at 1:18 am

Hi, I could use this script to get the emails in my mailbox, But do I get the messages in any mailbox. How can i specify the alias or smtp address of the mailbox to get the emails within the mailbox

#Load the API DLL
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"

#Create a new service object
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

# Fancy Way to Check the user and dynamically assign the Autodiscover URL
$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://"
$aceuser = [ADSI]$sidbind

#Do some stuff

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
$viewCount = 60
$view = New-Object Microsoft.Exchange.WebServices.Data.ItemView($viewCount)
$findResults = $service.FindItems([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$view)

$i = 0
#$cpsFound = 0
while ($i -le ($viewCount - 1))
$currdate = Get-Date
    #$findResults.Items[$i].Id |fl *
    if($findResults.Items[$i].DateTimeSent.ToShortDateString() -eq $currdate.ToShortDateString())
        if($findResults.Items[$i].Subject.Contains("STRING_UR_LOOKING_4") -eq $True)
        "Subject : " + $findResults.Items[$i].Subject

        $EmailItemID = $findResults.Items[$i].Id
        $currEmailItem = [Microsoft.Exchange.WebServices.Data.Item]::Bind($service,$EmailItemID)   
        $currSubject = $findResults.Items[$i].Subject
        $myParseCmd = $currSubject.Split("|")

     # Increment the while loop 
        $i +=1

This script was taken from

July 19, 2018 at 7:10 am

This is not a PS issue. It is a permissions issue in O365/AAD.

Unless you are a global admin in O365/AAD or Exchange Admin or have been granted the Exchange Application Impersonation role rights, you cannot look into another users mailbox. Looking into, acting on another user mailbox requires delegated right directly or via Exchange AppImpersonation.

Imagine the security implications / ramifications if any random user / dev to just pop open anyone mailbox. Not a good thing.

MS provides many docs on Exchange AppImpersonation.


July 19, 2018 at 12:18 pm

I am already an admin and have full rights to all user mailboxes.

Any ideas on the correct syntax?

July 20, 2018 at 6:54 pm

I was able to get autodiscover to work, but $inbox returns the Inbox of the admin account.
How can i get it to return the Inbox of the mobileprint shared mailbox.
Any ideas?

[Reflection.Assembly]::LoadFile("C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll")
$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$AdminName = ""
$Pass = Get-Content "cred_.txt" | ConvertTo-SecureString
$s.Credentials = New-Object Net.NetworkCredential($AdminName , $Pass)
$s.AutodiscoverUrl("", {$true})
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)