Enhancing Security Compliance with PowerShell in M365
In the modern digital workplace, maintaining security compliance is paramount. This PowerShell script automates the retrieval of security compliance information from Microsoft 365, helping administrators to assess the state of their organizational compliance configurations quickly and efficiently. This script will: 1. Connect to Microsoft 365 services. 2. Retrieve and display compliance details such as audit logs, data loss prevention (DLP) rules, and user permissions. 3. Generate a report outlining compliance settings. By automating these compliance checks, organizations can ensure they are meeting necessary security requirements without the hassle of manual audits.
# Install the required module if not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
Install-Module Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "AuditLog.Read.All","Compliance"
# Get audit logs
$auditLogs = Get-MgAuditLogSignIn -Top 10
Write-Host "=== Recent Audit Logs ==="
foreach ($log in $auditLogs) {
Write-Host "User: $($log.UserPrincipalName), Status: $($log.Status), Time: $($log.CreatedDateTime)"
}
# Get Data Loss Prevention (DLP) rules
$dlpRules = Get-MgDataLossPreventionPolicy
Write-Host "=== Data Loss Prevention Rules ==="
foreach ($rule in $dlpRules) {
Write-Host "DLP Rule: $($rule.DisplayName), Status: $($rule.Status)"
}
# Get user permissions
$users = Get-MgUser
Write-Host "=== User Permissions ==="
foreach ($user in $users) {
Write-Host "User: $($user.DisplayName), Roles: $($user.AssignedLicenses | Measure-Object).Count"
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Compliance report generation completed."Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in Microsoft 365 & Entra ID
Automate M365 User License Assignment with PowerShell
2025-01-09Manage M365 Users with PowerShell
2025-01-09Automate Microsoft 365 User License Management with PowerShell
2025-01-09Streamlining M365 User Management with PowerShell
2025-01-09PowerShell Script to Retrieve M365 User License Info
2025-01-09Automate Microsoft 365 User Report Generation with PowerShell
2025-01-09Ready when you are.
Try ServerEngine free for 7 days.