Automating SharePoint Site Permissions Audit with PowerShell

Managing and auditing site permissions in SharePoint is crucial to ensure that sensitive information is protected and that users have appropriate access. This PowerShell script automates the process of retrieving and displaying permissions for a specified SharePoint site. With this script, administrators can easily conduct regular audits to maintain security and compliance within their SharePoint environment.
This script will:
1. Connect to the SharePoint site using PowerShell.
2. Retrieve the list of users with permissions for the specified site.
3. Display the permissions assigned to each user.
By leveraging this script, IT administrators can save time on permission management and enhance the integrity of access control within SharePoint.

# Install SharePoint Online Management Shell if not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Online.SharePoint.PowerShell)) {
    Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser -AllowClobber
}
# Connect to SharePoint Online
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
$credential = Get-Credential
Connect-SPOService -Url $siteUrl -Credential $credential
# Retrieve the site groups and their permissions
$groups = Get-SPOSiteGroup -Site $siteUrl
Write-Host "=== Permissions Audit for Site: $siteUrl ==="
foreach ($group in $groups) {
    Write-Host "Group: $($group.Title)"
    $users = Get-SPOGroupMembers -Group $group.Title -Site $siteUrl
    foreach ($user in $users) {
        Write-Host "  User: $($user.LoginName)"
    }
}
# Disconnect from SharePoint
Disconnect-SPOService
Write-Host "Permissions audit completed."