0

I'm trying to write a PowerShell script that automates the way to retrieve all my emails with sender information in outlook and importing it on a text file.

I monitored this script that I created returns incomplete results.

Below here is my code for:

$namespace = $Outlook.GetNameSpace("MAPI")

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

$emails = $inbox.items


ForEach ($email in $emails){    
   write-host $email.Subject

}
  • 1
    What are you expecting Just the sender names, or names and subject, or header info? Your post only shows the subject, so, you are not asking for names, thus they will not be returned. There are lots of sample scripts (all over the web) and videos on Youtube to show you how to interact with Outlook via PowerShell to make ti do stuff and get stuff from it. – postanote Sep 12 '20 at 05:13
  • Sorry for the confusion my that script above listed all the subject of the email. As per checking the Subject of all my email in outlook vs the script I wrote doest match up. Like there is 1,500 email in my outlook but it only returns like half of it – Cedric Miraflor Sep 12 '20 at 05:18

2 Answers2

1

If "incomplete results" means that it's not returning all the emails you're expecting, there are a couple of things that I ran into when working with emails in Powershell:

  1. It won't grab emails that are in folders under the Inbox. You have to call each folder separately. I had to setup a recusive loop to compile a list of them
  2. Not all of your emails are actually stored in Outlook. By default, Outlook only pulls the last year of email form an email server. Sometimes it can show messages that exist on the server but they aren't actually downloaded.

EDIT: Here's the recursive function I built to get all the folders and subfolders within the Inbox.

# Create an ArrayList and immediately add the Inbox as the first folder in the list

[System.Collections.ArrayList] $folderList = @([PSCustomObject]@{
    FolderPath = $inbox.FolderPath
    EntryID = $inbox.EntryID
})

# Call the function to get all the folders and subfolders in the Inbox folder
Get-MailFolders $inbox.Folders

# Recusive function that will get all the folders and subfolders in the parent folder
function Get-MailFolders ($parent) {
    foreach ($child in $parent) {
        Write-Host "." -NoNewLine
        $folderList.Add([PSCustomObject]@{
            FolderPath = $child.FolderPath
            EntryID = $child.EntryID
        }) | Out-Null
        Get-MailFolders ($child.Folders)
    }
}
catherinemohan
  • 361
  • 1
  • 7
  • This actually is very helpful. For number 1 I actually want to grab my whole default inbox folder. For number 2 Im curious how can I pull the emails that are stored in the email server? Is there a way i can save all my email? – Cedric Miraflor Sep 12 '20 at 05:23
  • 1
    As for your server ask; unless your email admin allows you to do this, you can't. If your org only allows OST, which means what you are seeing in Outlook, is what is on the server. Now, if they allow you to use PST, and are doing mail archiving, then sure, you could have mail in your local PST with an archive on the mail server. You do not get to the mail server directly using Outlook, you have to use the Exchange cmdlets or the Exchange EWS API to get to mail. Email policy governs what you can and cannot get to. – postanote Sep 12 '20 at 05:30
  • 1
    1. If you want to grab the whole inbox folder, child folders and all, you'll need a recursive loop to do so. I have an example function that I can upload later. It's on a different machine than I normally use. – catherinemohan Sep 12 '20 at 06:05
  • 2. You can force Outlook to download all Exchange emails by changing the Offline Settings. Some companies may restrict this like @postanote details, but I haven't seen that type of policy configured at any of the places I've worked. To change the Offline Settings, open the Account Settings, click the email account you're using, and click the Change button. To force Outlook to download all of the emails from a server, uncheck the "Use Cached Exchange Mode" checkbox. All your emails will be downloaded to your computer. Alternatively, you can move the slider to increase the time past 1 year. – catherinemohan Sep 12 '20 at 06:10
  • This will be very helpful. As per checking, you are right about your statement 1. i have a directory that looks like this Test@sample.com test@sample.com\inbox test@sample.com\inbox\reports *sorry there is no new line in stackoverflow – Cedric Miraflor Sep 12 '20 at 06:45
  • Updated my answer to add the recursive function I used for getting the folders and subfolders in my Inbox. – catherinemohan Sep 14 '20 at 16:49
0

Continuing form my comment Search for:

'PowerShell read outlook email name and subject'

hit(s)

https://devblogs.microsoft.com/scripting/use-powershell-to-data-mine-your-outlook-inbox

Read most recent e-mail from outlook using PowerShell

http://jon.glass/blog/reads-e-mail-with-powershell

olFolderInbox = 6
$outlook = new-object -com outlook.application;
$mapi = $outlook.GetNameSpace("MAPI");
$inbox = $mapi.GetDefaultFolder($olFolderInbox)

# Grab the specific properties from the messages in the Inbox:
$olFolderInbox = 6
$outlook = new-object -com outlook.application;
$mapi = $outlook.GetNameSpace("MAPI");
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$inbox.items|Select SenderEmailAddress,to,subject|Format-Table -AutoSize

# Results
<#
SenderEmailAddress                   To                             Subject
------------------                   --                             -------
notify@twitter.com                   Jonathan Glass                 Thomas Garnier (@mxatone) retweeted one...
mailing-list@rifftrax.com            Riff                           Rediscover Puppets in our latest short!
notify@twitter.com                   Jonathan Glass                 [ Gunther ] (@Gunther_AR) retweeted one...
#>
postanote
  • 15,138
  • 2
  • 14
  • 25