The Microsoft graph API is based on Web API. So if you want to download an attachment you can use the HTTP GET command directly with the Microsoft Graph API Get Attachment API.
In PowerShell, if you want to use an HTTP call natively, you can use Invoke-WebRequest (for more details you can see How can I use HTTP GET in PowerShell?).
But Microsoft provides some facilities (modules) for making Microsoft Graph API easy to use in PowerShell. That say Microsoft.Graph Module. This module is in the preview stage, but it is very useful.
If you want to use this module and download an attachment, you can use the below command:
Get-MgUserMessageAttachment -MessageId <String> -UserId <String> [-Count] [-Expand <String[]>]
[-Filter <String>] [-Orderby <String[]>] [-Search <String>] [-Select <String[]>] [-Skip <Int32>]
[-Top <Int32>] [<CommonParameters>]
For more information about this command, you can see the Microsoft Graph SDK PowerShell documentation.
Get-MgUserMessageAttachment
returns IMicrosoftGraphAttachment
that has some properties like below (JSON representation):
{
"contentType": "string",
"id": "string (identifier)",
"isInline": true,
"lastModifiedDateTime": "String (timestamp)",
"name": "string",
"size": 1024
}
But this type is based type of fileAttachment
that has some properties like below (JSON representation):
{
"contentBytes": "string (binary)",
"contentId": "string",
"contentLocation": "string",
"contentType": "string",
"id": "string (identifier)",
"isInline": true,
"lastModifiedDateTime": "String (timestamp)",
"name": "string",
"size": 1024
}
For file download, you can use the ContentBytes
property. This property is Base64 encoded so you have to decode it and then save to a file with some of commands like Out-File.
As you can see in the GitHub repository of PowerShell Module, in the current state of this PowerShell you cannot use it directly with this SDK to download an attachment, and you have to use Web API directly in PowerShell for downloading the attachment.