Reading emails from chosen mail folder

by Zushakon at 2012-11-16 04:49:31


I was not sure if this topic should be here or in Powershell general so please excuse me if i should be in a diffrent forum.

I’m trying to search thru mails in one folder in my mailbox(or pst file). My problem is that i don’t know how to change to diffrent folder.

$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")

$folder = $namespace.getDefaultFolder($olFolders::olfoldersentmail)
$folder.items | Select-Object -Property Subject, SentOn, Importance, To

I’m know i can change for example olfolderinboxmail or sentmail etc. But how to change for example to folder "koza" in my personal folder ?
by Pat Richard at 2012-11-16 10:36:04
So, OlDefaultFolders Enumeration shows that Microsoft.Office.Interop.Outlook.olDefaultFolders only applies to, well, the default folders in a mailbox. I went nosing through Microsoft.Office.Interop.Outlook Namespace and couldn’t find a solution to let you look in non-default folders.

I’ll keep looking.
by ArtB0514 at 2012-11-20 13:43:02
$namespace.Folders | foreach {$.Name} will get you the names of all the folders in your namespace. You can then get one of those folders with $folder = $namespace.Folders.Item("NameOfYourFolder") You might have to execute this pair sequentially (recursively) in order to find the subfolder that you really want.
by Pat Richard at 2012-11-20 21:12:06
When I use $namespace.Folders | foreach {$.Name}, I get the top level for the various accounts I have configured (Two Exchange accounts, each with an archive). I don’t get a list of the folders within each.
So how do you specify which top level folder to look in when using $folder = $namespace.Folders.Item("NameOfYourFolder")?
by ArtB0514 at 2012-11-21 07:22:55
The first line gets you a list of all top level folders. You need to decide which one is the one you want to use. Put its name in the second line then look into that object’s folders property to get the sub folders. Do this recursively until you find the one you really want. This will list all your top level folders and the first level child folders in each:
$namespace.Folders | foreach {
"Processing top level folder $($.Name)"
.Folders | foreach {" Contains child folder $($_.Name)"}