How to Discover and enumerate Exchange Online SendAs and SendOnBehalfOf Permissions
Valentin Komarovskiy, MBA
Solving complex business problems and alleviating technical pain points to improve efficiency and reduce risks.
In Exchange Online (part of Microsoft 365), Send on Behalf and Send As are two distinct permissions that control how users can send emails on behalf of others.
1. Send on Behalf Permission
2. Send As Permission
Assigning Send As or Send on Behalf of permissions is not that difficult and most admins assign those rights via Exchange Control Panel. However, when you need to enumerate which users have SendOnBehalfOf permissions assigned to, this becomes a problem as there is no reverse enumeration in Exchange Online or PowerShell. What I mean is that you can go to a mail user and see who they delegate send as or send on behalf of too, but not who delegates those permissions to them.
The following script will find all mailbox with SendOnBehalfOf permissions within the Exchange Online tenant
#this outputs all SendOnBehalfOf permissions in Exchange Online Tenant
#First 3 commands are to install module and connect with Exchange Admin creds, make sure you got correct creds
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
# Define the user you want to check for "Send on Behalf Of" permissions
$sendOnBehalfUser = "[email protected]"
# Define the output file path, make sure you have a folder called C:\Temp
$outputFile = "C:\Temp\SendOnBehalfResults.txt"
# Initialize the output file with a header
"Mailboxes that ${sendOnBehalfUser} can send on behalf of:" | Out-File -FilePath $outputFile
# Retrieve all mailboxes with no result size limit
$mailboxes = Get-Mailbox -ResultSize Unlimited
# Loop through each mailbox and check if [email protected] is in the GrantSendOnBehalfTo list
foreach ($mailbox in $mailboxes) {
# Get the GrantSendOnBehalfTo property for the current mailbox
$sendOnBehalfList = (Get-Mailbox -Identity $mailbox.Identity | Select-Object -ExpandProperty GrantSendOnBehalfTo)
# Check each entry in GrantSendOnBehalfTo for a match
foreach ($entry in $sendOnBehalfList) {
# Match the property where [email protected] appears (e.g., PrimarySmtpAddress)
if ($entry.PrimarySmtpAddress -eq $sendOnBehalfUser) {
# Append the mailbox display name to the output file if permissions are found
$mailbox.DisplayName | Out-File -FilePath $outputFile -Append
break
}
}
}
# Optional: Display a message confirming the file path
Write-Output "Results saved to $outputFile"
The following script will find all mailbox with SendOnBehalfOf permissions and dump then into a text file located in C:\temp\SendOnBehalfResults.txt
#this outputs all SendOnBehalfOf permissions in Exchange Online Tenant
#First 3 commands are to install module and connect with Exchange Admin creds, make sure you got correct creds
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
# Define the SamAccountName or identifier to check for in "Send on Behalf Of" permissions, samaccountname should be replaced with the SAM of the account to whome permssions have been granted, the trustee
$sendOnBehalfUser = "samaccountname"
# Define the output file path, make sure C:\temp folder exists
$outputFile = "C:\temp\SendOnBehalfResults.txt"
# Initialize the output file with a header
"Mailboxes that ${sendOnBehalfUser} can send on behalf of:" | Out-File -FilePath $outputFile
# Retrieve all mailboxes with no result size limit
$mailboxes = Get-Mailbox -ResultSize Unlimited
# Loop through each mailbox and check if noreply can send on behalf
foreach ($mailbox in $mailboxes) {
# Get the GrantSendOnBehalfTo property for the current mailbox
$sendOnBehalfList = Get-Mailbox -Identity $mailbox.Identity | Select-Object -ExpandProperty GrantSendOnBehalfTo
# Check each entry in GrantSendOnBehalfTo for the matching SamAccountName
foreach ($entry in $sendOnBehalfList) {
# Check if the entry's SamAccountName or other identifier matches "noreply"
if ($entry.SamAccountName -eq $sendOnBehalfUser) {
# Append the mailbox display name to the output file if permissions are found
$mailbox.DisplayName | Out-File -FilePath $outputFile -Append
break
}
}
}
# Optional: Display a message confirming the file path
Write-Output "Results saved to $outputFile"
This checks who has send as permissions to a specific mailbox, in this instance the mailbox is ?[email protected]
#this outputs all SendOnBehalfOf permissions in Exchange Online Tenant
#First 3 commands are to install module and connect with Exchange Admin creds, make sure you got correct creds
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
# Define a specific mailbox to inspect GrantSendOnBehalfTo entries
$mailboxIdentity = "[email protected]”
# Display the full GrantSendOnBehalfTo property
Get-Mailbox -Identity $mailboxIdentity | Select-Object -ExpandProperty GrantSendOnBehalfTo
The following script will output all SendAS permissions of all mailboxes in the Exchange tenant
# Connect to Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
# Get all mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited
# Initialize a string to store results
$results = ""
# Loop through each mailbox and get the "Send As" permissions
foreach ($mailbox in $mailboxes) {
# Retrieve the Send As permissions for the mailbox
$sendAsPermissions = Get-RecipientPermission -Identity $mailbox.Identity | Where-Object { $_.Trustee -ne 'NT AUTHORITY\SELF' -and $_.AccessRights -contains 'SendAs' }
# Add the results to the string if there are Send As permissions
foreach ($permission in $sendAsPermissions) {
$results += "Mailbox: $($mailbox.DisplayName) - GrantedTo: $($permission.Trustee) - AccessRight: Send As`n"
}
}
# Output the results to a text file
$results | Out-File -FilePath "C:\temp\SendAsPermissions2.txt" -Encoding UTF8
# Disconnect from Exchange Online
Disconnect-ExchangeOnline
Solving complex business problems and alleviating technical pain points to improve efficiency and reduce risks.
2 周Brent Foley - check out my masterpiece scripts for Exchange!