PowerShell/Scripts/check-outlook.ps1

32 lines
781 B
PowerShell
Raw Normal View History

2022-06-09 16:35:26 +02:00
<#
.SYNOPSIS
2022-10-12 15:12:38 +02:00
Checks Outlook's inbox
2022-06-09 16:35:26 +02:00
.DESCRIPTION
2022-10-12 15:12:38 +02:00
This PowerShell script checks the inbox of Outlook for new/unread mails.
2022-06-09 16:35:26 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-outlook.ps1
No new mails.
2022-06-09 16:35:26 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$Inbox = $Namespace.GetDefaultFolder(6) # 6 = olFolderInbox
[int]$Unread = 0
foreach($Mail in $Inbox.Items) {
if ($Mail.Unread -eq $false) { continue }
2022-10-12 15:12:38 +02:00
"⚠️ New mail '$($Mail.Subject)' from $($Mail.SenderName)."
2022-06-09 16:35:26 +02:00
$Unread++
}
2022-10-12 15:12:38 +02:00
if ($Unread -eq 0) { "✅ No new mails." }
2022-06-09 16:35:26 +02:00
exit 0 # success
} catch {
"Sorry: $($Error[0])"
exit 1
2023-08-06 21:35:36 +02:00
}