Ghawblin
Ghawblin

Reputation: 71

How to target outlook subfolder using powershell

I'm trying to filter about 2000 automated alerts in an outlook sub-folder.

I need to do the following series of steps:

  1. Parse sub-folder Account Alert Lockouts
  2. Search for a specific phrase that has a variable username
  3. Dump out that whole phrase with the variable username into csv

Example Phrase

Account Name:       jdoe

I have all of the required emails in a sub-folder, I just need to analyze them.

I've gotten my code to work in the Inbox, but it doesn't cover the sub-folder.

Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")

$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)    

$RE = [RegEx]'(?sm)Account Name\s*:\s*(?<AccName>.*?)$.*'

$DebugPreference = 'Continue'

$Data = foreach ($item in $inbox.items) {
    if ($item.body -match $RE) {
        Write-Host "ding "
        [PSCustomObject]@{ AccName = $Matches.AccName }
    }
}

$Data 
$Data | Export-CSv '.\data.csv' -NoTypeInformation

Upvotes: 3

Views: 3757

Answers (1)

Lews Therin
Lews Therin

Reputation: 3777

Per the documentation for NameSpace.GetDefaultFolder:

To return a specific non-default folder, use the Folders collection.

And the documentation for the Folders collection referenced above:

Use Folders (index), where index is the name or index number, to return a single Folder object. Folder names are case-sensitive.

You should be able to add this:

$subfolder = $inbox.Folders('Account Alert Lockouts')

and change your foreach to iterate over $subfolder.

Upvotes: 2

Related Questions