PowerShell! Copilot is your very best friend.
And yes, this... requires it. The GUI just can't do this as far as I know.
Copilot Prompt
Code:
please help me create a powershell script that can query M365 Exchange Online, and generate a list of email boxes that have email forwarding configured to target a specific mailbox.
Code:
# Define the target forwarding address
$TargetForwardingAddress = "target@example.com"
# Connect to Exchange Online (Ensure Exchange Online module is installed)
Connect-ExchangeOnline -UserPrincipalName admin@example.com
# Retrieve all mailboxes with forwarding enabled
$MailboxesWithForwarding = Get-Mailbox | Where-Object { $_.ForwardingSMTPAddress -ne $null }
# Filter mailboxes that forward to the specific address
$FilteredMailboxes = $MailboxesWithForwarding | Where-Object { $_.ForwardingSMTPAddress -eq $TargetForwardingAddress }
# Display results
$FilteredMailboxes | Select-Object DisplayName, PrimarySMTPAddress, ForwardingSMTPAddress | Format-Table -AutoSize
# Disconnect from Exchange Online session
Disconnect-ExchangeOnline -Confirm:$false
Of this code, I focus on the 2nd command... Because it's the bit that gets me a list of all email boxes that have forwarding enabled. This seems to be a good report to have in the stack of snips, so I'm changing gears to generate a full forwarding report I can open an massage in Excel!
Get-Mailbox | Where-Object { $_.ForwardingSMTPAddress -ne $null }
Back to Copilot, Prompt:
Code:
given this command: Get-Mailbox Where-Object { $_.ForwardingSMTPAddress -ne $null }
How do I extend this output to list the UPN as well as the forwarding address?
Answer:
Code:
Get-Mailbox | Where-Object { $_.ForwardingSMTPAddress -ne $null } | Select-Object DisplayName, UserPrincipalName, ForwardingSMTPAddress | Format-Table -AutoSize
A quick execution against my own tenant returns a nice table of all users with forwards and targets! Perfection! Copilot inferred that DisplayName would be useful as well, and added that for me, unprompted!
One more quick tweak... This bit I do myself because I've done this 1000 times... I swear I live in Excel these days!
Code:
Get-Mailbox | Where-Object { $_.ForwardingSMTPAddress -ne $null } | Select-Object DisplayName, UserPrincipalName, ForwardingSMTPAddress | Export-Csv -Path "C:\Temp\MailboxForwardingReport.csv" -NoTypeInformation
And now I have a PowerShell snippet that can generate CSV exports of any tenant's forwarding configuration, open it up in Excel, format as a table and filter to find whatever specifics I want.
Please note, this action was completed via the free Copilot available as an app on any Windows 11 machine, as well as what's available for free at
https://copilot.microsoft.com
$0 was spent to have AI assist me in creating this report, total time investment. ~5min.
I'm not personally capable of writing PowerShell myself, but I can read most programming languages and understand them. For those that do not know powershell, please remember that Get- commands are READ commands, they are safe to execute. If it doesn't work as expected, the risk of damage is virtually nonexistent! Experiment!
Now, when you start mucking about with the set- commands... there... be the dragons! Keep it short, keep it simple, take a moment to read and understand. And most importantly, never stop learning!