Optimizing Security Compliance Checks in Microsoft 365 with PowerShell
Maintaining security compliance is critical for protecting sensitive information and adhering to regulatory requirements in any organization. This PowerShell script automates the process of retrieving compliance settings and security details within Microsoft 365, enabling administrators to assess and enhance their security posture effectively. This script will: 1. Connect to Microsoft Graph API. 2. Retrieve compliance policies currently in place. 3. Output any compliance alerts to help identify areas needing attention. By using this script, IT professionals can streamline compliance audits and ensure that their organization is securely managing its data within Microsoft 365.
# Install Microsoft.Graph module if not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
Install-Module -Name Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'Compliance.Read.All'
# Retrieve compliance policies
$compliancePolicies = Get-MgCompliancePolicy
Write-Host "=== Microsoft 365 Compliance Policies ==="
foreach ($policy in $compliancePolicies) {
Write-Host "Policy Name: $($policy.DisplayName) - Status: $($policy.Status)"
}
# Check for compliance alerts
$complianceAlerts = Get-MgComplianceAlert
Write-Host "=== Compliance Alerts ==="
if ($complianceAlerts) {
foreach ($alert in $complianceAlerts) {
Write-Host "Alert: $($alert.AlertType) - Severity: $($alert.Severity) - Created: $($alert.CreatedDateTime)"
}
} else {
Write-Host "No compliance alerts found."
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Compliance check 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.