How to read Office 365 Outlook attachments using powershell script?

New Contributor


We have email account and users daily send emails with attachments to that email account and automated powershell script was running on the windows server to retrieve data from email attachments . Script was working fine but now we are getting authentication token issue since our Outlook was migrated to Office 365.


May I know your suggestions to resolve the issue?

I am totally new to azure Office 365 and Azure.


Thank you,


8 Replies
If you have migrated to Office 365, you might need to change in your scripts the way you authenticate against the service

Thank you for your comments but my problem is, I do not have access for admin site and I am not sure what kind of information I should get from admin team.


Without seeing the actual script, we can only guess what's going on. My guess would be that the script is using EWS impersonation, as you cannot access item-level details with "regular" PowerShell. Thus you most likely need to add the impersonation permissions on the migrated account. But that's just a guess, best talk with the author of the script.

Hi Vasil,


Here is my script, it is working fine but I want to use Date Range instead of ArgumentList in FindItems method (highlighted in script). May I know which property I should use for Date Range in FindItems?




# Set the path to your copy of EWS Managed API
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
# Load the Assembly

# Create a new Exchange service object
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService

#These are your O365 credentials
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($mail,$password)

# this TestUrlCallback is purely a security check
$TestUrlCallback = {
param ([string] $url)
if ($url -eq "") {$true} else {$false}
# Autodiscover using the mail address set above

$results = $service.FindItems(
( New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 20)
$MailItems = $results.Items | where hasattachments

foreach ($MailItem in $MailItems){


foreach($Attachment in $MailItem.Attachments){
$File = new-object System.IO.FileStream(("C:\Temp\Attachments\" + $Attachment.Name.ToString()),[System.IO.FileMode]::Create)
$File.Write($attachment.Content, 0, $attachment.Content.Length)


Thank you,


Thank you Vasil. I used search filter to get Date Range results.


I used search filter to get Date Range results.


Would you mind to share the piece of code you used for the search filter based on date?





Hi @AA_007 , @Vasil Michev , @matloe ,
You guys know a way to catch the "To" field of an email? We're forwarding emails to an end-user's mailbox and want to catch the email address used in To, where the email was originally sent to.

Many thanks