Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Generating Compliance Reports using PnP PowerShell

Posted on March 18, 2025March 18, 2025 by Rishan Solutions

Loading

1. Introduction

Organizations using SharePoint Online must ensure compliance with security, privacy, and data retention regulations. PnP PowerShell helps generate compliance reports to:

✔ Monitor user permissions and access
✔ Track external sharing activity
✔ Audit Data Loss Prevention (DLP) policies
✔ Review retention policies and expired documents
✔ Log SharePoint activities for security audits

By automating compliance reporting, organizations can detect security risks, ensure regulatory compliance, and optimize data governance.


2. Prerequisites

Before generating compliance reports, ensure:

✅ PnP PowerShell is installed

Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force

You have SharePoint Administrator or Compliance Administrator permissions
Retention, DLP, and security policies are configured in Microsoft Purview Compliance Center
PowerShell has access to Microsoft 365 audit logs


3. Connecting to SharePoint Online Using PnP PowerShell

To run compliance reports, first connect to your SharePoint Online tenant:

Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive

✔ This prompts for secure authentication.


4. Types of Compliance Reports

PnP PowerShell can generate various compliance-related reports, including:

✔ User Permissions Report – Who has access to what?
✔ External Sharing Report – Tracking externally shared files
✔ DLP Policy Report – Identifying sensitive data exposure
✔ Audit Logs Report – Reviewing user activities in SharePoint
✔ Retention Policies Report – Checking expired and retained files

Each report helps maintain compliance, security, and governance.


5. Generating Compliance Reports

A. Auditing User Permissions

To generate a report on who has access to SharePoint sites:

$Sites = Get-PnPTenantSite  
$Report = @()

foreach ($Site in $Sites) {
Connect-PnPOnline -Url $Site.Url -Interactive
$Users = Get-PnPUser

foreach ($User in $Users) {
$Report += [PSCustomObject]@{
SiteURL = $Site.Url
User = $User.Email
Role = $User.Roles
}
}
}

$Report | Export-Csv -Path "C:\Reports\SharePointPermissions.csv" -NoTypeInformation
Write-Host "User permissions report generated successfully!"

✔ This report helps identify unauthorized access.


B. Monitoring External Sharing

To track files shared externally:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteUrl -Interactive

$SharedFiles = Get-PnPListItem -List "Documents" | Where-Object { $_["SharingStatus"] -eq "External" }

$SharedFiles | Select-Object FieldValues.FileLeafRef, FieldValues.FileRef, FieldValues.ModifiedBy | Export-Csv -Path "C:\Reports\ExternalSharing.csv" -NoTypeInformation

Write-Host "External sharing report generated successfully!"

✔ Identifies security risks from external sharing.


C. Checking Data Loss Prevention (DLP) Policies

To verify if DLP policies are correctly applied:

$DLPPolicies = Get-PnPTenantDlpPolicy  
$DLPPolicies | Select-Object Name, Mode, LastModifiedTime | Export-Csv -Path "C:\Reports\DLPReport.csv" -NoTypeInformation

Write-Host "DLP policy report generated successfully!"

✔ Ensures sensitive data is protected.


D. Exporting SharePoint Audit Logs

To review who accessed or modified documents:

$StartDate = (Get-Date).AddDays(-30)
$EndDate = Get-Date

$AuditLogs = Search-UnifiedAuditLog -StartDate $StartDate -EndDate $EndDate -RecordType SharePointFileOperation -ResultSize 5000

$AuditLogs | Select-Object CreationDate, UserIds, Operations, ObjectId | Export-Csv -Path "C:\Reports\AuditLogs.csv" -NoTypeInformation

Write-Host "SharePoint audit log report generated successfully!"

✔ Helps detect suspicious activities.


E. Generating Retention Policy Reports

To check expired and retained files:

$RetentionFiles = Get-PnPListItem -List "Documents" | Where-Object { $_["RetentionLabel"] -ne $null }

$RetentionFiles | Select-Object FieldValues.Title, FieldValues.RetentionLabel, FieldValues.Modified | Export-Csv -Path "C:\Reports\RetentionPolicies.csv" -NoTypeInformation

Write-Host "Retention policy report generated successfully!"

✔ Ensures compliance with data retention policies.


6. Automating Compliance Report Generation

To schedule compliance reports daily:

1️⃣ Save your script as C:\Scripts\ComplianceReports.ps1
2️⃣ Open Task Scheduler
3️⃣ Click Create Basic Task
4️⃣ Set Trigger to Daily at 2:00 AM
5️⃣ Set Action to Start a Program
6️⃣ Enter powershell.exe in Program/Script
7️⃣ In Add Arguments, enter:

-File "C:\Scripts\ComplianceReports.ps1"

8️⃣ Click Finish to enable automation

✔ Automatically generates and exports compliance reports every day.

Posted Under PNP PowerShellaudit logs Automation Compliance Reports Data Loss Prevention external sharing Microsoft 365 PNP PowerShell Security SharePoint online

Post navigation

System.TimeoutException – The operation has timed out
IoT in Digital Health Records Management

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions