# Load the Exchange Online module if not already loaded
if (-not (Get-Module -Name ExchangeOnlineManagement)) {
Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber
}
# Connect to Exchange Online (you will need to provide credentials)
Write-Host "Connecting to Exchange Online..."
$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName -ShowProgress $true
# Function to get inbox rules, including hidden, for a specific mailbox
function Get-MailboxRules {
param (
[string]$Mailbox
)
# Get the inbox rules for the mailbox
$rules = Get-InboxRule -Mailbox $Mailbox -ErrorAction SilentlyContinue -IncludeHidden
# Return the rules along with the mailbox name and other information about the rule
return $rules | Select-Object @{Name='Mailbox';Expression={$Mailbox}}, Name, From, Description, SubjectContainsWords, BodyContainsWords, HeaderContainsWords, MyNameInToBox, ForwardTo, RedirectTo, MoveToFolder, DeleteMessage, CopyToFolder, SendTextMessageNotificationTo, Enabled, IsHidden, Priority, Action, Condition
}
# Get all mailboxes in the tenant
$allMailboxes = Get-Mailbox -ResultSize Unlimited
# Initialize an array to hold all rules
$allRules = @()
# Loop through each mailbox and retrieve its inbox rules
foreach ($mailbox in $allMailboxes) {
Write-Host "Retrieving inbox rules for mailbox: $($mailbox.UserPrincipalName)"
$mailboxRules = Get-MailboxRules -Mailbox $mailbox.UserPrincipalName
$allRules += $mailboxRules
}
# Specify the output file path
$outputFile = "C:\temp\MailboxInboxRules.csv"
# Export the rules to a CSV file
Write-Host "Exporting inbox rules to file: $outputFile"
$allRules | Export-Csv -Path $outputFile -NoTypeInformation -Force
# Disconnect from Exchange Online
Disconnect-ExchangeOnline -Confirm:$false
Write-Host "Script completed. All inbox rules are exported to $outputFile."