-
Notifications
You must be signed in to change notification settings - Fork 2
/
export-messages-with-graph-powershell.ps1
51 lines (42 loc) · 2.19 KB
/
export-messages-with-graph-powershell.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# export-messages-with-graph-powershell.ps1
# atwork.at, Toni Pohl, Christoph Wilfing
# One-time process: Install the Graph module
Install-Module Microsoft.Graph -Scope CurrentUser
# Or update the existing module to the latest version
# Update-Module Microsoft.Graph
# Check the cmdlets
# Get-InstalledModule Microsoft.Graph
Import-Module Microsoft.Graph.Mail
# Connect with Mail.Read permissions
Connect-MgGraph -Scopes "Mail.Read"
# Show the user context just as info
Get-MgContext
# get your user id - insert your own primary email address here
$user = Get-MgUser -Filter "UserPrincipalName eq '<your-email-address>'"
# Get a list of all mail folders
$folders = Get-MgUserMailFolder -UserId $user.Id -All
# Select the Inbox
$inbox = $folders | Where-Object { $_.DisplayName -eq "Inbox" }
# Get a list of all sub folders of the Inbox
$childs = Get-MgUserMailFolderChildFolder -UserId $user.Id -MailFolderId $inbox.Id -All
# Select the desired folder
$myfolder = $childs | Where-Object { $_.DisplayName -eq "<your-subfolder>" }
# Get all mails and export them (add an optional where filter if needed).
# We remove all HTML tags, repair line breaks and HTML spaces to get a readable text in the result file.
Get-MgUserMailFolderMessage -All `
-UserId $user.Id `
-MailFolderId $myfolder.Id | `
Select-Object `
@{N = 'Received'; E = { $_.ReceivedDateTime } }, `
@{N = 'Sender'; E = { $_.Sender.foreach{ ($_.Emailaddress) }.address } }, `
@{N = 'ToRecipient'; E = { $_.ToRecipients.foreach{ ($_.Emailaddress) }.address } }, `
@{N = 'ccRecipient'; E = { $_.ccRecipients.foreach{ ($_.Emailaddress) }.address } }, `
@{N = 'Subject'; E = { $_.Subject } }, `
@{N = 'Importance'; E = { $_.Importance } }, `
@{N = 'Body'; E = { ($_.Body.Content -replace '</p>',"`r`n" -replace "<[^>]+>",'' -replace " ",' ').trim() } } | `
Where-Object {( ($_.Subject -notlike "*newsletter*") -and ($_.Subject -notlike "*FYI*") ) } | `
Export-Csv ".\mails.csv" -Delimiter "`t" -Encoding utf8
# End. Check the mails.csv file.
# Best, open it with Microsoft Excel: Menu Data, From Text/CSV and follow the wizard.
# Disconnect when done
Disconnect-MgGraph