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."