Need an example PowerShell script to get attachments from an O365 Exchange Online Shared Mailbox

Valued Contributor

I need an example PowerShell script to get attachments from an O365 Exchange Online Shared Mailbox.  Or even a regular user mailbox.


Anyone got one available or know where I can find one?

6 Replies

You have two options here: use some EWS based script (Glen's blog is usually the best resource for anything EWS related: or use the Outlook COM object model if you have a single shared mailbox that you have configured in Outlook (example here

Thanks, I ended up formulating the solution based on a solution from @Eli Van Eenwyk


$cred = Get-Credential

$sharedMailbox = ""
$url = "$sharedMailbox/messages"
$date = "2016-08-03"

## Get all messages that have attachments where received date is greater than $date 
$messageQuery = $url + "?`$select=Id&`$filter=HasAttachments eq true and DateTimeReceived ge " + $date
$messages = Invoke-RestMethod $messageQuery -Credential $cred

## Loop through each results
foreach ($message in $messages.value){

    # get attachments and save to file system
    $query = $url + "/" + $message.Id + "/attachments"
    $attachments = Invoke-RestMethod $query -Credential $cred

    # in case of multiple attachments in email
    foreach ($attachment in $attachments.value){
        $path = "c:\Temp\" + $attachment.Name
        $Content = [System.Convert]::FromBase64String($attachment.ContentBytes)
        Set-Content -Path $path -Value $Content -Encoding Byte

    # Move processed email to a subfolder
    $query = $url + "/" + $message.Id + "/move"
    Invoke-RestMethod $query -Body $body -ContentType "application/json" -Method post -Credential $cred


Oh right, we also have REST APIs now :) Gotta start learning those...

What is your opinion of the API's, and graph as a whole. I like the dev talks i go to about it, but have struggled to find a use for them outside of just doing my powershell scripts. Granted I do not application development, but ya.... curious as to your thought Vasil.

I am getting AUTH errors.  I am using my Global Admin account and my account for the inbox I am trying to extract attachment from.


My use case is a bit different.  I need to automate a barrage of emails we received from our FirePower device.  In short, the number of emails/attachments are too numerous to process manually.


I would like to save each attachment from a senderaddress to a folder on my computer.  


I am thinking that I need to create a Graph API.  

@QuentinLMcCallum-AWA Power Automate (former Flow) has templates for this and can be done within seconds if that helps.