2020-01-22 14:32:11 +05:30
<# Purpose : Enable mailbox audit logging for all Office 365 mailboxes
2025-03-01 17:29:12 +05:30
Last updated : Feb 22 , 2025
2020-01-22 14:32:11 +05:30
Website : https : / / O365reports . com
2023-09-28 12:40:25 +05:30
Script Highlights :
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
1 . You can enable mailbox audit logging for all mailboxes using single cmdlet
2 . This script allows you to enable mailbox auditing for all mailbox actions or set of actions
3 . The script can be executed with MFA enabled account also .
4 . This script automatically installs Exchange Online PowerShell Module upon your confirmation
2020-01-22 14:32:11 +05:30
For execution steps and usecases : https : / / o365reports . com / 2020 / 01 / 21 / enable-mailbox -auditing -in -office - 365 -powershell
#>
2022-10-22 14:35:38 +05:30
2020-01-22 14:32:11 +05:30
#Accept input paramenters
param (
[ Parameter ( Mandatory = $false ) ]
[ string ] $UserName ,
[ string ] $Password ,
2025-03-01 17:29:12 +05:30
[ string ] $Organization ,
[ string ] $ClientId ,
[ string ] $CertificateThumbprint ,
2022-10-22 14:35:38 +05:30
[ ValidateSet ( 'ApplyRecord' , 'Copy' , 'Create' , 'FolderBind' , 'HardDelete' , 'MailItemsAccessed' , 'MessageBind' , 'Move' , 'MoveToDeletedItems' , 'RecordDelete' , 'SearchQueryInitiated' , 'Send' , 'SendAs' , 'SendOnBehalf' , 'SoftDelete' , 'Update' , 'UpdateCalendarDelegation' , 'UpdateComplianceTag' , 'UpdateFolderPermissions' , 'UpdateInboxRules' , 'MailboxLogin' ) ]
[ string[] ] $Operations = ( 'ApplyRecord' , 'Copy' , 'Create' , 'FolderBind' , 'HardDelete' , 'MailItemAccessed' , 'MessageBind' , 'Move' , 'MoveToDeletedItems' , 'RecordDelete' , 'SearchQueryInitiated' , 'Send' , 'SendAs' , 'SendOnBehalf' , 'SoftDelete' , 'Update' , 'UpdateCalendarDelegation' , 'UpdateComplianceTag' , 'UpdateFolderPermissions' , 'UpdateInboxRules' , 'MailboxLogin' )
2020-01-22 14:32:11 +05:30
)
2025-03-01 17:29:12 +05:30
2022-10-22 14:35:38 +05:30
Function Connect_Exo
{
2025-03-01 17:29:12 +05:30
#Check for EXO module inatallation
2022-10-22 14:35:38 +05:30
$Module = Get-Module ExchangeOnlineManagement -ListAvailable
if ( $Module . count -eq 0 )
2020-01-22 14:32:11 +05:30
{
2025-03-01 17:29:12 +05:30
Write-Host Exchange Online PowerShell module is not available -ForegroundColor yellow
2022-10-22 14:35:38 +05:30
$Confirm = Read-Host Are you sure you want to install module ? [ Y] Yes [N ] No
if ( $Confirm -match " [yY] " )
2020-01-22 14:32:11 +05:30
{
2022-10-22 14:35:38 +05:30
Write-host " Installing Exchange Online PowerShell module "
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
2020-01-22 14:32:11 +05:30
}
else
{
2025-03-01 17:29:12 +05:30
Write-Host EXO module is required to connect Exchange Online . Please install module using Install-Module ExchangeOnlineManagement cmdlet .
2022-10-22 14:35:38 +05:30
Exit
}
2020-01-22 14:32:11 +05:30
}
2022-10-22 14:35:38 +05:30
Write-Host Connecting to Exchange Online . . .
#Storing credential in script for scheduling purpose/ Passing credential as parameter - Authentication using non-MFA account
if ( ( $UserName -ne " " ) -and ( $Password -ne " " ) )
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System . Management . Automation . PSCredential $UserName , $SecuredPassword
2025-03-01 17:29:12 +05:30
Connect-ExchangeOnline -Credential $Credential -ShowBanner: $false
}
elseif ( $Organization -ne " " -and $ClientId -ne " " -and $CertificateThumbprint -ne " " )
{
Connect-ExchangeOnline -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -Organization $Organization -ShowBanner: $false
2022-10-22 14:35:38 +05:30
}
else
{
2025-03-01 17:29:12 +05:30
Connect-ExchangeOnline -ShowBanner: $false
2022-10-22 14:35:38 +05:30
}
}
Connect_Exo
2020-01-22 14:32:11 +05:30
$MBCount = 0
2022-10-22 14:35:38 +05:30
$AuditAdmin = " ApplyRecord " , " Copy " , " Create " , " FolderBind " , " HardDelete " , " MailItemsAccessed " , " Move " , " MoveToDeletedItems " , " RecordDelete " , " Send " , " SendAs " , " SendOnBehalf " , " SoftDelete " , " Update " , " UpdateCalendarDelegation " , " UpdateComplianceTag " , " UpdateFolderPermissions " , " UpdateInboxRules "
$AuditDelegate = " ApplyRecord " , " Create " , " FolderBind " , " HardDelete " , " MailItemsAccessed " , " Move " , " MoveToDeletedItems " , " RecordDelete " , " SendAs " , " SendOnBehalf " , " SoftDelete " , " Update " , " UpdateComplianceTag " , " UpdateFolderPermissions " , " UpdateInboxRules "
$AuditOwner = " ApplyRecord " , " Create " , " HardDelete " , " MailItemsAccessed " , " MailboxLogin " , " Move " , " MoveToDeletedItems " , " RecordDelete " , " SearchQueryInitiated " , " Send " , " SoftDelete " , " Update " , " UpdateCalendarDelegation " , " UpdateComplianceTag " , " UpdateFolderPermissions " , " UpdateInboxRules "
2020-01-22 14:32:11 +05:30
2022-10-22 14:35:38 +05:30
if ( $Operations . Length -eq 21 )
2020-01-22 14:32:11 +05:30
{
$RequiredOperations = $Operations
2025-03-01 17:29:12 +05:30
Get-EXOMailbox -ResultSize Unlimited | Select PrimarySmtpAddress , DisplayName | ForEach {
2020-01-22 14:32:11 +05:30
$DisplayName = $_ . Displayname
Write-Progress -Activity " `n Processed mailbox count: $MBCount " ` n " Currently Processing: $DisplayName "
$MBCount + +
Set-Mailbox -Identity $_ . PrimarySmtpAddress -AuditEnabled $true -AuditAdmin $AuditAdmin -AuditDelegate $AuditDelegate -AuditOwner $Auditowner
}
}
else
{
$RequiredOperations = $PSBoundParameters . Operations
[ System.Collections.ArrayList ] $EnableAuditAdmin = @ ( )
[ System.Collections.ArrayList ] $EnableAuditDelegate = @ ( )
[ System.Collections.ArrayList ] $EnableAuditOwner = @ ( )
Foreach ( $Operation in $RequiredOperations )
{
if ( $AuditAdmin -match $Operation )
{
$EnableAuditAdmin + = $Operation
}
if ( $AuditDelegate -match $Operation )
{
$EnableAuditDelegate + = $Operation
}
if ( $AuditOwner -match $Operation )
{
$EnableAuditOwner + = $Operation
}
}
2025-03-01 17:29:12 +05:30
Get-EXOMailbox -ResultSize Unlimited | Select PrimarySmtpAddress , DisplayName | ForEach {
2020-01-22 14:32:11 +05:30
$DisplayName = $_ . Displayname
Write-Progress -Activity " `n Processed mailbox count: $MBCount " ` n " Currently Processing: $DisplayName "
$MBCount + +
Set-Mailbox -Identity $_ . PrimarySmtpAddress -AuditEnabled $true -AuditAdmin $EnableAuditAdmin -AuditDelegate $EnableAuditDelegate -AuditOwner $EnableAuditowner
}
}
2023-09-28 12:40:25 +05:30
Write-Host ` nMailbox Audit logging enabled for $MBCount mailboxes -ForegroundColor Yellow ` n
2020-01-22 14:32:11 +05:30
Write-Host " Mailbox Audit Logging enabled following operation(s): " $RequiredOperations
2023-09-28 12:40:25 +05:30
Write-Host ` n ~ ~ Script prepared by AdminDroid Community ~ ~ ` n -ForegroundColor Green
Write-Host " ~~ Check out " -NoNewline -ForegroundColor Green ; Write-Host " admindroid.com " -ForegroundColor Yellow -NoNewline ; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~ " -ForegroundColor Green ` n ` n