none
How can I access an Outlook folder with Powershell other than the default?

    Question

  • I have come up with a simple script to examine Outlook for specific emails, group by subject, sort and save the results to a .csv file. See below for the script. I need to figure out how to do this for a different data store. Unless I copy the emails I am looking for into my main .pst, I run into errors trying to access the alternate store. The secondary store is loaded and accessible from the same session as the one my main .pst is loaded into.

    # Instantiate a new Outlook object
    $ol = new-object -comobject "Outlook.Application";
    
    # Map to the MAPI namespace
    $mapi = $ol.getnamespace("mapi");
    
    # Get a list of the Inbox folder items in the $Items variable
    $Items = $Mapi.GetDefaultFolder(6).Items;
    
    # Get the SenderName and Subject
    $items | select SenderName, SentOn, Subject | # Get name, date, subject
    Where {$_.SenderName -Match "2010"} |         # Filter by email
    Group-Object -Property Subject |              # Group by subject
    Sort Count -Descending |                      # Sort on subject count
    Select count, name |                          # Get only Count, name
    ft -auto |                                    # Show full columns
    Export-Csv -Path "C:\test\SCEEmailList.csv" -NoTypeInformation -Encoding ASCII -Delimiter ','                 # Save results to .csv
    
    

    When I do try to navigate to the second store with a line like this everything works:

    # Get a list of the Inbox folder items in the $Items variable
    $folders = $Mapi.Folders[2] | Select FolderPath;
    FolderPath
    ----------
    \\Personal Folders - Old
    

    When I try to go down a folder level, I get what seems like a viable result:

    PS > $folders = $Mapi.Folders[2].Folders | select folderpath;
    PS > $folders
    
    FolderPath
    ----------
    FolderPath
    ----------
    \\Personal Folders - Old\Deleted Items
    \\Personal Folders - Old\Inbox
    \\Personal Folders - Old\Outbox
    \\Personal Folders - Old\Sent Items
    \\Personal Folders - Old\Calendar
    \\Personal Folders - Old\Contacts
    \\Personal Folders - Old\Journal
    \\Personal Folders - Old\Notes
    \\Personal Folders - Old\Tasks
    \\Personal Folders - Old\Drafts
    \\Personal Folders - Old\Junk E-mail
    \\Personal Folders - Old\Quarantine
    \\Personal Folders - Old\RSS Feeds
    \\Personal Folders - Old\TO BE RESOLVED
    \\Personal Folders - Old\Projects
    

    but, when I try to operate at this level I have problems: # Try to gain access to the \\Personal Folders - Old\Inbox (down just 1 level) PS > $folders[2].Items

    This should returns results, but, I get nothing when I type

    $folders

    Being more explicit (and removing the variable, I get this:

    PS > $mapi.Folders[2].Folders[2]
    Unable to index into an object of type System.__ComObject.
    At line:1 char:26
    + $mapi.Folders[2].Folders[ <<<< 2]
        + CategoryInfo          : InvalidOperation: (2:Int32) [], RuntimeException
        + FullyQualifiedErrorId : CannotIndex
    

    I tried to see if I could get any information from the command with gm to no avail. I got the same error. I decided to go back up a level and see what gm returned and save this, which, to me, suggests I should still be able to access the subfolders:

    PS > $mapi.Folders[2] | gm
    
    
       TypeName: System.__ComObject#{00063006-0000-0000-c000-000000000046}
    
    Name                   MemberType Definition
    ----                   ---------- ----------
    AddToFavorites         Method     void AddToFavorites (Variant, Variant)
    AddToPFFavorites       Method     void AddToPFFavorites ()
    CopyTo                 Method     MAPIFolder CopyTo (MAPIFolder)
    Delete                 Method     void Delete ()
    Display                Method     void Display ()
    GetCalendarExporter    Method     CalendarSharing GetCalendarExporter ()
    GetExplorer            Method     _Explorer GetExplorer (Variant)
    GetStorage             Method     _StorageItem GetStorage (string, OlStorageIdentifierType)
    GetTable               Method     Table GetTable (Variant, Variant)
    MoveTo                 Method     void MoveTo (MAPIFolder)
    AddressBookName        Property   string AddressBookName () {get} {set}
    Application            Property   _Application Application () {get}
    Class                  Property   OlObjectClass Class () {get}
    CurrentView            Property   View CurrentView () {get}
    CustomViewsOnly        Property   bool CustomViewsOnly () {get} {set}
    DefaultItemType        Property   OlItemType DefaultItemType () {get}
    DefaultMessageClass    Property   string DefaultMessageClass () {get}
    Description            Property   string Description () {get} {set}
    EntryID                Property   string EntryID () {get}
    FolderPath             Property   string FolderPath () {get}
    Folders                Property   _Folders Folders () {get}
    FullFolderPath         Property   string FullFolderPath () {get}
    InAppFolderSyncObject  Property   bool InAppFolderSyncObject () {get} {set}
    IsSharePointFolder     Property   bool IsSharePointFolder () {get}
    Items                  Property   _Items Items () {get}
    MAPIOBJECT             Property   IUnknown MAPIOBJECT () {get}
    Name                   Property   string Name () {get} {set}
    Parent                 Property   IDispatch Parent () {get}
    PropertyAccessor       Property   PropertyAccessor PropertyAccessor () {get}
    Session                Property   _NameSpace Session () {get}
    ShowAsOutlookAB        Property   bool ShowAsOutlookAB () {get} {set}
    ShowItemCount          Property   OlShowItemCount ShowItemCount () {get} {set}
    Store                  Property   Store Store () {get}
    StoreID                Property   string StoreID () {get}
    UnReadItemCount        Property   int UnReadItemCount () {get}
    UserDefinedProperties  Property   UserDefinedProperties UserDefinedProperties () {get}
    UserPermissions        Property   IDispatch UserPermissions () {get}
    Views                  Property   _Views Views () {get}
    WebViewAllowNavigation Property   bool WebViewAllowNavigation () {get} {set}
    WebViewOn              Property   bool WebViewOn () {get} {set}
    WebViewURL             Property   string WebViewURL () {get} {set}
    

    Am I missing something obvious here?

    Sunday, September 04, 2011 6:51 AM

Answers

  • Hi Will,

    Then try get it like this:

     

    $mapi.Folders.Item("Personal Folders - Old").Folders.Item("Inbox")
    

     or that:

    $mapi.Folders.Item(2).Folders.Item(2)

    • Edited by MichalGajdaMVP Sunday, September 04, 2011 7:20 AM
    • Marked as answer by Will Steele Sunday, September 04, 2011 8:04 AM
    Sunday, September 04, 2011 7:18 AM

All replies

  • Hi Will,

    Then try get it like this:

     

    $mapi.Folders.Item("Personal Folders - Old").Folders.Item("Inbox")
    

     or that:

    $mapi.Folders.Item(2).Folders.Item(2)

    • Edited by MichalGajdaMVP Sunday, September 04, 2011 7:20 AM
    • Marked as answer by Will Steele Sunday, September 04, 2011 8:04 AM
    Sunday, September 04, 2011 7:18 AM
  • Hi Will,

    Then try get it like this:

     

    $mapi.Folders.Item("Personal Folders - Old").Folders.Item("Inbox")
    

     or that:

     

    $mapi.Folders.Item(2).Folders.Item(2)
    

     

    That got it.  A few more sets of Folders.Item, but, this got me where I needed to go.  Thank you.
    Sunday, September 04, 2011 8:04 AM
  • Any chance you could post the revised script again?
    Sunday, October 21, 2012 1:26 PM