Skip to content

Instantly share code, notes, and snippets.

@dhcgn
Created June 7, 2024 17:57
Show Gist options
  • Save dhcgn/2628df9aba01e50c7001bac5e081f587 to your computer and use it in GitHub Desktop.
Save dhcgn/2628df9aba01e50c7001bac5e081f587 to your computer and use it in GitHub Desktop.
This powershell script searches for emails in the Outlook inbox that match a specified address pattern and displays their email addresses.
<#
.SYNOPSIS
This script searches for emails in the Outlook inbox that match a specified address pattern and displays their email addresses.
.DESCRIPTION
The script first checks if the Microsoft.Office.Interop.Outlook DLL is present.
If not, it downloads and extracts the DLL from NuGet to ensure the script can interact with Outlook.
Then, it adds the Outlook assembly, creates an Outlook application object, and accesses the MAPI namespace to interact with the inbox.
It searches through all emails in the inbox and checks each recipient's email address against a specified pattern.
If a match is found, it prints the email address.
.PARAMETER searchPattern
The email address pattern to search for in the recipient's email addresses. Default is set to "*gmail*".
Modify the $searchPattern variable in the script to change the search pattern.
.EXAMPLE
.\SearchOutlookEmails.ps1
This example runs the script using the default settings, where it searches for email addresses containing "gmail".
.INPUTS
None. You cannot pipe objects to this script.
.OUTPUTS
String
Outputs the email addresses that match the specified pattern. Errors during the search are caught and a message is displayed,
indicating the subject and received time of the email where the error occurred.
.NOTES
Version: 1.0
The script requires that Outlook is installed on the machine where the script is executed.
It is designed to work with Outlook folders accessible to the user running the script.
.LINK
Documentation for Microsoft.Office.Interop.Outlook: https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook
#>
if (-not (Test-Path "Microsoft.Office.Interop.Outlook.dll")) {
# Download the Outlook Interop Assembly from NuGet
$url = "https://www.nuget.org/api/v2/package/Microsoft.Office.Interop.Outlook/15.0.4797.1004"
$pathInZip = "lib\net20\Microsoft.Office.Interop.Outlook.dll"
# Download
Invoke-WebRequest -Uri $url -OutFile "Microsoft.Office.Interop.Outlook.zip"
# Extract
Expand-Archive -Path "Microsoft.Office.Interop.Outlook.zip" -DestinationPath "Microsoft.Office.Interop.Outlook"
# Copy the DLL
Copy-Item -Path "Microsoft.Office.Interop.Outlook\$pathInZip" -Destination "Microsoft.Office.Interop.Outlook.dll"
# remove the zip and the folder
Remove-Item -Path "Microsoft.Office.Interop.Outlook.zip"
Remove-Item -Path "Microsoft.Office.Interop.Outlook" -Recurse
}
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
# ##################################
$searchPattern = "*gmail*"
# ##################################
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$emails = $inbox.Items
$count = 0
$emails | ForEach-Object {
# Progress every 100 Mails
$count++
if ($count % 100 -eq 0) {
Write-Progress -Activity "Searching Inbox" -Status "Processed $count emails" -PercentComplete ($count / $emails.Count * 100)
}
$mail = $_
$_.Recipients | ForEach-Object {
$empfaenger = $_
try {
$emailAdress = $empfaenger.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E")
if ($emailAdress -like $searchPattern) {
Write-Host $emailAdress
# Uncomment if only one email should be found
# exit
}
}
catch {
Write-Host "Skipping email with error, Subject: $($mail.Subject), ReceivedTime: $($mail.ReceivedTime)"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment