Skip to content

Download Email Attachments from Office 365 using Powershell script.

February 7, 2019

Here is a script that lets you download mail objects with attachments from an Exchange mailbox (works with Office 365). First, install Exchange Web Services Managed API 2.2.


# 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

$PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)

$datef = ‘2019-01-01’
$datet = get-date($dtt) -format ‘yyyy-MM-dd’
write-host $datef
write-host $dtf
write-host $dtt
write-host $datet

# Set up folder path locations for the search filter
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$mail)
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
# Create the search filter
$Sfgt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $datef)
$Sflt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $datet)
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);

# Perform the search
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(100)
$results = $Inbox.FindItems($sfCollection,$view)


# Withou Search Filter
#$results = $service.FindItems(“Inbox”,( New-Object #Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 20)

$MailItems = $results.Items | where hasattachments
foreach ($MailItem in $MailItems)
#$targetTime = get-date($MailItem.DateTimeReceived) -Format ‘yyyy-MM-dd’


write-host $MailItem.Name
write-host $MailItem.DateTimeReceived
write-host $MailItem.Subject
write-host $MailItem.From
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)


Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: