All scripts
Automation 953

Backup-SharePointSitePermissions

Exports the full permission structure of a SharePoint site, groups and their members, to a JSON file before you make a bulk permissions change you might need to reverse.

Backup-SharePointSitePermissions.ps1
<#
.SYNOPSIS
    Backs up a SharePoint site's permission structure to JSON.

.DESCRIPTION
    Exports every SharePoint group on a site along with its members and
    permission level, giving you a restore reference before making a
    bulk permissions change you might need to reverse.

.PARAMETER SiteUrl
    URL of the site to back up.

.PARAMETER BackupPath
    Folder to write the backup file to. Defaults to the current directory.

.EXAMPLE
    .\Backup-SharePointSitePermissions.ps1 -SiteUrl https://contoso.sharepoint.com/sites/finance

.NOTES
    Requires PnP.PowerShell.

.AUTHOR
    Shehryar Hassan
#>

param(
    [Parameter(Mandatory)]
    [string]$SiteUrl,
    [string]$BackupPath = (Get-Location).Path
)

Connect-PnPOnline -Url $SiteUrl -Interactive
$groups = Get-PnPGroup

$backup = foreach ($group in $groups) {
    $members = Get-PnPGroupMember -Group $group
    [pscustomobject]@{
        GroupName       = $group.Title
        PermissionLevels = (Get-PnPGroupPermissions -Identity $group).Name -join ", "
        Members         = $members.Title
    }
}

$file = Join-Path $BackupPath "sp-permissions-backup_$(Get-Date -Format yyyyMMdd_HHmmss).json"
$backup | ConvertTo-Json -Depth 5 | Out-File -FilePath $file -Encoding utf8
Write-Host "Backed up permissions for $($groups.Count) group(s) to $file" -ForegroundColor Cyan

Read it before you run it, and test in a safe tenant first.