

{"id":614,"date":"2024-03-10T17:14:34","date_gmt":"2024-03-10T16:14:34","guid":{"rendered":"https:\/\/blog.koeckeis-fresel.net\/?p=614"},"modified":"2024-03-10T17:25:38","modified_gmt":"2024-03-10T16:25:38","slug":"m365-get-all-forwarding-rules","status":"publish","type":"post","link":"https:\/\/blog.koeckeis-fresel.net\/?p=614","title":{"rendered":"m365 get all forwarding rules"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># Temporarily set the execution policy to bypass for this session\nSet-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force\n\n# Check for the ExchangeOnlineManagement module and update or install as necessary\n$module = Get-Module -Name ExchangeOnlineManagement -ListAvailable\n\nif ($module -ne $null) {\n    # Module is installed, attempt to update it\n    Write-Host \"ExchangeOnlineManagement module is installed. Checking for updates...\"\n    Update-Module -Name ExchangeOnlineManagement -Force\n} else {\n    # Module is not installed, install it\n    Write-Host \"ExchangeOnlineManagement module is not installed. Installing...\"\n    Install-Module -Name ExchangeOnlineManagement -Force\n}\n\n# Confirm the module is up to date\nWrite-Host \"ExchangeOnlineManagement module is up to date.\"\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># Connect to Exchange Online\nConnect-ExchangeOnline -UserPrincipalName youradminuser@yourdomain.com\n\n# After connection, retrieve the domain from the connected session\n$userPrincipalName = (Get-ExoMailbox -Identity youradminuser@yourdomain.com).UserPrincipalName\n$domain = $userPrincipalName.Split(\"@\")&#91;1]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$mailboxes = Get-Mailbox -ResultSize Unlimited\n$forwardingRules = @()\n\nforeach ($mailbox in $mailboxes) {\n    $rules = Get-InboxRule -Mailbox $mailbox.Identity | Where-Object { $_.ForwardTo -or $_.ForwardAsAttachmentTo -or $_.RedirectTo }\n    foreach ($rule in $rules) {\n        $obj = New-Object PSObject -Property @{\n            Mailbox = $mailbox.PrimarySmtpAddress\n            RuleName = $rule.Name\n            ForwardTo = $rule.ForwardTo\n            ForwardAsAttachmentTo = $rule.ForwardAsAttachmentTo\n            RedirectTo = $rule.RedirectTo\n        }\n        $forwardingRules += $obj\n    }\n}\n\n$mailboxForwarding = Get-Mailbox -ResultSize Unlimited | Where-Object { $_.ForwardingSmtpAddress -or $_.ForwardingAddress } | Select-Object DisplayName, ForwardingSmtpAddress, ForwardingAddress<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># Ensure the output directory exists\n$OutputDirectory = \"C:\/temp\/\"\nIf (-not (Test-Path -Path $OutputDirectory)) {\n    New-Item -ItemType Directory -Force -Path $OutputDirectory\n}\n\n# Export forwarding rules to a text file\n$forwardingRules | Format-Table | Out-File -FilePath \"${OutputDirectory}${domain}_forwardingRules.txt\"\n\n# Export mailbox-level forwarding to a text file\n$mailboxForwarding | Format-Table | Out-File -FilePath \"${OutputDirectory}${domain}_mailboxForwarding.txt\"<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-614","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/posts\/614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=614"}],"version-history":[{"count":0,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=\/wp\/v2\/posts\/614\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.koeckeis-fresel.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}