Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / How to Read Outlook Emails with PowerShell

April 24, 2024 Microsoft OfficePowerShell

How to Read Outlook Emails with PowerShell

Let’s look at how to open, read, and parse emails in a connected Outlook mailbox from within a PowerShell script. Outlook MAPI allows you to directly access the mailbox, list mailbox items, and read the emails (including the sender’s address, subject, body, etc.).

This scenario requires that Outlook is installed on the computer and that there is a configured mailbox profile. You can read a mailbox from any mail server, including Exchange, Outlook.com, Gmail, AOL, Yahoo, and more.

In order for PowerShell to have access to the contents of the mailbox, you must have Outlook running on the computer. Check whether the outlook.exe process is running and run it in the background by using the command:

$OutlookProc = ( Get-Process | where { $_.Name -eq "OUTLOOK" } )
if ( $OutlookProc -eq $null ) { Start-Process outlook.exe -WindowStyle Hidden; Start-Sleep -Seconds 5 }

Now you need to load the .NET class and create an Outlook instance:

Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application

The contents of the mailbox are accessible via the MAPI protocol namespace:

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

There can be multiple folders in a mailbox. List mailbox folders:

$NameSpace.Folders.Item(1).Folders | FT FolderPath

You can display a list of folders in a tree view and count the number of email items in each folder:

Function Listfolders
{ 
  param($Folders, $Indent)
  ForEach ($Folder in $Folders | sort-object name)
  {
    write-host $Indent$($Folder.Name)" ("$($Folder.Items.Count)")"
    Listfolders $Folder.Folders $Indent"  " 
  }
}
ListFolders $namespace.Folders ""

PowerShell: List folders in Outlook mailbox

To find out the default name of the folder for incoming emails, run the following command (by default, this is the Inbox folder, but this name may vary depending on your language/regional settings):

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

List emails in the Inbox folder in the following format: sender address, recipient, subject, size, and date received.

$inbox.Items | ft SenderEmailAddress, To, Subject, Size, ReceivedTime

List emails in Outlook Inbox folder

Simple PowerShell Where-Object filter allows you to search for specific emails. For example, list emails received today from a specific sender:

$currentDate = Get-Date
$inbox.Items |  Where-Object { $_.ReceivedTime -like "*$currentDate*"   -and $_.SenderEmailAddress -eq "[email protected]"}

You can display the subject and body of the email. The email body can be displayed in plain text (Body property) or in HTML format (HTMLBody property). In this example, the body of the most recently received e-mail will be displayed:

$inbox.Items($inbox.Items.Count)|select SenderEmailAddress,subject,Body,HTMLBody|fl

View Outlook email message body with PowerShell

To check if the email has been read by the Exchange user, the UnRead attribute can be used.

If the email has an attachment, you can save the attachment file to a local drive:

$email= $inbox.Items($inbox.Items.Count)
if ($Email.Attachments.Count -gt 0) {
$Attachment = $Email.Attachments.Item(1)
$Attachment.SaveAsFile("C:\Downloads\$($Email.Attachments.Item(1).FileName)")
}

Delete the last received email from the mailbox:

$email= $inbox.Items($inbox.Items.Count)
$Email.Delete()

You can use PowerShell to access your Outlook mailbox in automation scripts that require you to perform specific actions when you receive an incoming email. Use the Task Scheduler task to run a PS1 script to check your mailbox.

0 comment
0
Facebook Twitter Google + Pinterest
previous post
Hide Library and Special Folders from File Explorer on Windows
next post
Configure DNS Scavenging to Clean Up Stale DNS Records in AD

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Remove ‘Your License isn’t Genuine’ Banner in MS...

April 21, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Microsoft Key Management Service (KMS) Volume Activation FAQs
  • Automatic Outlook User Profile Configuration with ZeroConfigExchange
  • How to Copy/Paste to MS Word without Losing Formatting
  • Deploying Microsoft Office Language Packs
  • Rollback Microsoft Office to an Earlier Version After an Update
  • Hardware Graphics Acceleration Causes Visual Glitches in Microsoft Office Apps
  • Remove ‘Your License isn’t Genuine’ Banner in MS Office
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top