I am trying to extract email metadata from messages in an Outlook Exchange account. I derive various metadata as described here, but I can't get the name of the folder where the email has been stored.
What I have so far:
Clear-Host
$outlook = New-Object -Com Outlook.Application
$mapi = $outlook.GetNamespace('MAPI')
$mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent
$walkFolderScriptBlock = {
param(
$currentFolder
)
foreach ($item in $currentFolder.Folders) {
$item.Items
}
}
$Email = & $walkFolderScriptBlock $mailboxRoot
$Results = $Email | Select ConversationTopic, ReceivedTime;
$Results | Export-Csv -Path C:\Temp\2024-01-25EmailTesting.csv
I tried variants of the word "folder" following the word "Select". It returns an empty column.
Update - @mhu has made a breakthrough and gotten the names of folders. When I run their script, I see folders like "Delete" and "Inbox". Which is a major step forward, but am wondering if I could also get the subfolders under "Inbox" (i.e. "Accessibility Committee" and "Advisory").


You can select the parent name property like this:
(and you don't need the script block, unless you want to recursively process the folders)
Example output:
Advanced example for processing nested folders: