0

How does one specify a shared email account during a PowerShell script? I'm trying to get metadata from a shared email account using this code (designed for a default email account):

   Set-StrictMode -Version "Latest"
$ErrorActionPreference = "Stop"

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
                $items += $item | Select-Object -Property "ConversationTopic", "ReceivedTime", @{ "Label" = "Folder"; "Expression" = { $_.Parent.Name } }
            }
        }

        # process (sub)folder items
        $items += Get-MailFromOutlookFolder -ParentFolder $folder
    }

    return $items    
}

$outlook     = New-Object -Com "Outlook.Application"
$mapi        = $outlook.GetNamespace("MAPI")
$mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent
$results = Get-MailFromOutlookFolder -ParentFolder $mailboxRoot
$results | Export-Csv -Path "C:\Temp\email.csv"

I've made four attempts to solve this problem:

1.

$mailboxRoot = $mapi.Folder(1).Parent

(In place of the line $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent)

Error code: Method invocation failed because [Microsoft.Office.Interop.Outlook.NameSpaceClass] does not contain a method named 'GetFolder'.

  1. Based on the info here.

Set-Location -Path 'C:\Users\<username>\AppData\Local\Microsoft\Outlook\<emailaddressOfSharedAccount>'

Error code: Set-Location : Cannot find path (Path definitely exists).

  1. Based on the info here.

Set FldrIn = FldrInbox.Folders("C:\Users\<userName>\AppData\Local\Microsoft\Outlook\<sharedInboxEmailAddress>.ost")

Error code:Set-Variable : A positional parameter cannot be found that accepts argument 'FldrInbox.Folders'.

  1. Based on the info here.

GetSharedDefaultFolder (Microsoft.Office.Interop.Outlook.Recipient Recipient, Microsoft.Office.Interop.Outlook.OlDefaultFolders FolderType)

Error code: Microsoft.Office.Interop.Outlook.Recipient : The term 'Microsoft.Office.Interop.Outlook.Recipient' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Update: It seems like I need to update the code here.

3
  • What is it you're trying to do exactly? Can you share your script? Commented Feb 27, 2024 at 17:46
  • Thanks for helping. I clarified the script above and my goal. Commented Feb 27, 2024 at 17:51
  • oops - yes the variable was missing. Commented Feb 27, 2024 at 18:09

2 Answers 2

0

This variable is not being declared:

$mailboxRoot

This should resolve it:

$mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to get a shared inbox, you need to replace the line $mailboxRoot = $mapi.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Parent with $mapi.Folders.Item(3). Just keeping changing the number in the parenthesis till you hit your desired shared inbox.

Set-StrictMode -Version "Latest"
$ErrorActionPreference = "Stop"

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
                $items += $item | Select-Object -Property "ConversationTopic", "ReceivedTime", @{ "Label" = "Folder"; "Expression" = { $_.Parent.Name } }
            }
        }

        # process (sub)folder items
        $items += Get-MailFromOutlookFolder -ParentFolder $folder
    }

    return $items    
}




$outlook     = New-Object -Com "Outlook.Application"
$mapi        = $outlook.GetNamespace("MAPI")
$mailboxRoot = $mapi.Folders.Item(3)
$results = Get-MailFromOutlookFolder -ParentFolder $mailboxRoot
$results | Export-Csv -Path "C:\Temp\email.csv" 

2 Comments

If you know the name of the shared mailbox you just need to filter result. The $map.Folders object has a property called "Name". Just do a Where-Object to filter out the rest of the mailboxes.
If you put your map.folder/where-object code as an answer, I'll vote it up - it's much smarter than my approach of just randomly putting in numbers ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.