I have a script which extracts email metadata to a spreadsheet. The script works when I apply it to a relatively small email account (~9000 emails). It also works when I apply it to a big email account (~250,000 emails), but set the script to only extract a single metadata field.
It doesn't work when I apply it to the big email account (~250,000 emails) and attempt to extract multiple metadata fields. Instead, it outputs the following error code and an incomplete spreadsheet (only 33,146 emails captured).
Exception from HRESULT: 0xB5940115At C:\file\path\path\name.ps1:15 char:18+ foreach ($item in ($folder |Select-Object -ExpandProperty "I ...++ CategoryInfo :OperationStopped: (:) [], COMException+ FullyQualifiedErrorID: System.Runtime.InteropServices.COMExceptionException from HRESULT: 0xBBC40115At C:\file\path\path\name.ps1:13 char:14+ foreach ($folder in $ParentFolder.Folders)++ CategoryInfo :OperationStopped: (:) [], COMException+ FullyQualifiedErrorID: System.Runtime.InteropServices.COMExceptionException from HRESULT: 0xBFE40115At C:\file\path\path\name.ps1:13 char:14+ foreach ($folder in $ParentFolder.Folders)++ CategoryInfo :OperationStopped: (:) [], COMException+ FullyQualifiedErrorID: System.Runtime.InteropServices.COMException
What I have tried:
- Googling the error code number. Nothing specific came up.
- Googling the second line of the error code (FullyQualifiedErrorID). Nothing specific came up.
Here is the script in full. Any help welcome.
function Get-MailFromOutlookFolder{ [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [Object] $ParentFolder ) $items = @() foreach ($folder in $ParentFolder.Folders) { foreach ($item in ($folder | Select-Object -ExpandProperty "Items")) { if ($item.Class -eq 43) { # process email $PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" #$smtpAddress = $item.PropertyAccessor.GetProperty($PR_SMTP_ADDRESS) $items += $item | Select-Object -Property "ConversationTopic", "ReceivedTime", "SenderName", "ReceivedByName", "BCC", "CC","Size", "SenderEmailAddress", @{ "Label" = "Folder"; "Expression" = { $_.Parent.Name } }, @{name="Attachments";expression={$_.Attachments|%{$_.DisplayName}}} } } # process (sub)folder items $items += Get-MailFromOutlookFolder -ParentFolder $folder } return $items }$outlook = New-Object -Com "Outlook.Application"$mapi = $outlook.GetNamespace("MAPI")$recipient = $mapi.CreateRecipient("name@domain.ca")$folder = $mapi.GetSharedDefaultFolder($recipient, [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent$folder1 = $folder.parent.folders('Online Archive - name@domain.ca')$results = Get-MailFromOutlookFolder -ParentFolder $folder1$results | Export-Csv -Path "C:\Fakefolder\email_account_name_2025-06-20.csv"