Scheduling SharePoint Reports using PnP PowerShell

Loading

Scheduling SharePoint Reports using PnP PowerShell allows you to generate automated reports on site usage, permissions, storage, and lists at regular intervals. These reports can be saved as CSV files and emailed to administrators.


Step 1: Install and Import PnP PowerShell

Before you start, install PnP PowerShell (if not already installed):

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

Then, import the module:

Import-Module PnP.PowerShell

PnP PowerShell is ready!


Step 2: Connect to SharePoint

To access SharePoint Online, connect using:

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

🔹 Replace yourtenant with your Microsoft 365 tenant name.
🔹 Replace yoursite with your SharePoint site name.

Connected to SharePoint!


Step 3: Generate a SharePoint Report

Example: Exporting Site Collection Details to CSV

$ReportPath = "C:\Reports\SiteCollectionReport.csv"
$Sites = Get-PnPTenantSite

$Sites | Select-Object Url, Template, StorageUsageCurrent, LastContentModifiedDate | Export-Csv -Path $ReportPath -NoTypeInformation

Write-Host "Site Collection Report generated: $ReportPath"

🔹 This script retrieves all site collections and exports details like URL, Template, Storage Usage, and Last Modified Date to a CSV file.

Site Collection Report generated successfully!


Step 4: Schedule the Report using Task Scheduler

To automate the report generation, create a PowerShell script file (.ps1).

1️⃣ Save the script as SharePointReport.ps1 in C:\Scripts\
2️⃣ Open Windows Task Scheduler
3️⃣ Create a new Task
4️⃣ Set a Trigger → Daily, Weekly, or Monthly
5️⃣ Set an Action → Start a Program → powershell.exe
6️⃣ Add Arguments

-ExecutionPolicy Bypass -File "C:\Scripts\SharePointReport.ps1"

7️⃣ Save and Enable the Task

Report scheduled successfully!


Step 5: Email the Report Automatically

To send the report via email:

$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$From = "admin@yourtenant.com"
$To = "manager@yourtenant.com"
$Subject = "SharePoint Site Report"
$Body = "Attached is the latest SharePoint Site Collection Report."
$Attachment = "C:\Reports\SiteCollectionReport.csv"

Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential (Get-Credential) -Attachments $Attachment

Write-Host "Email sent successfully!"

🔹 Replace yourtenant.com with your organization’s email domain.
🔹 The script sends the CSV report as an email attachment.

Email notification sent successfully!


Step 6: Automating Other SharePoint Reports

1️⃣ Export SharePoint Permissions Report

$ReportPath = "C:\Reports\PermissionsReport.csv"
$Permissions = Get-PnPSiteCollectionAdmin

$Permissions | Export-Csv -Path $ReportPath -NoTypeInformation
Write-Host "Permissions report generated successfully!"

2️⃣ Export SharePoint List Data Report

$ListName = "Project Tasks"
$ReportPath = "C:\Reports\ListDataReport.csv"
$ListItems = Get-PnPListItem -List $ListName

$ListItems | Select-Object Id, Title, Created, Modified | Export-Csv -Path $ReportPath -NoTypeInformation
Write-Host "List Data Report generated!"

3️⃣ Export SharePoint Storage Usage Report

$ReportPath = "C:\Reports\StorageUsageReport.csv"
$Sites = Get-PnPTenantSite

$Sites | Select-Object Url, StorageUsageCurrent | Export-Csv -Path $ReportPath -NoTypeInformation
Write-Host "Storage usage report generated successfully!"

All reports scheduled successfully!

Leave a Reply

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