Caffeinated
Caffeinated

Reputation: 12484

Using Powershell, How to access outlook highest-level folders?

I want to obtain some data from within a folder called "FDA UFMS User Provision", my PS code is here:

$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderSentMail
$bigFolder = $sentMail.Parent


$ufms = "FDA UFMS User Provision"


$newufms = $bigFolder.folders.item($ufms)


$newufms.Items | %{ $RESULT=[Regex]::Match($_.TaskSubject, "Request\s\d{6}"); if ($RESULT.Success)

{$RESULT.Value}} | %{$Result=[Regex]::Match($_, "\d{6}"); if($RESULT.Success){$RESULT.Value}} | Out-File C:\Temp

\Powershell_6_digit_Codes.txt -Append

But the problem is that it is getting the "UFMS" folder which is under Inbox. However I want to access the higher-level "UFMS" folder (happens to be a shared network email box)

In the picture, the arrow points to the email box I want to access, which is called "FDA UFMS User Provision" and is level with the "Adel" outlook folder:

What I tried so far

$newuf = $outlook.Session.Folders.Item("FDA UFMS User Provision").item

$newuf.Items | | %{ $RESULT=[Regex]::Match($_.TaskSubject,"Request\s\d{6}"); if ($RESULT.Success){$RESULT.Value}} | %{$Result=[Regex]::Match($_, "\d{6}");if($RESULT.Success){$RESULT.Value}} | Out-File C:\Temp\Powershell_6_digit_Codes_ULF.txt -Append

enter image description here

thanks !

Upvotes: 1

Views: 934

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36277

Getting folders is really very simple. You simply have to walk down from the root account through the folder path one by one. Here is what you would need to do for that folder:

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $outlook.GetNameSpace("MAPI")
$MyAcct = $Namespace.Folders | ?{$_.Name -match $env:USERNAME}
$UFMS = $MyAcct.Folders | ?{$_.Name -match "FDA UFMS User Provision"}

That does assume that your user name is in your email address, so if my user name is TMTech then my email address would be something like [email protected].

If you wanted a folder inside that one, you would add a line like:

$OtherFldr = $UFMS.Folders | Where{$_.Name -match "Other Folder"}

Edit: Ok, I just noticed where you said it's a shared mailbox. In that case change what I just said... To find a mailbox other than your own we need to change the $MyAcct = line. So it would become:

$SharedMB = $NameSpace.Folders | Where{$_.Name -match "FDA UFMS User Provision"}

Now $SharedMB should be the root of that mailbox, then you would get the inbox as described above, or whatever folder you are looking for.

Upvotes: 2

Related Questions