PnP PowerShell is a powerful tool for IT Admins to automate and manage SharePoint tasks efficiently. Custom scripts help in:
Automating Site and User Management
Monitoring and Reporting SharePoint Usage
Managing Permissions and Content
Security and Compliance Audits
This guide provides step-by-step custom scripts for common IT admin tasks in SharePoint.
Step 1: Setting Up PnP PowerShell
1.1 Install PnP PowerShell
Run the following command in PowerShell:
Install-Module PnP.PowerShell -Scope CurrentUser
To update:
Update-Module PnP.PowerShell
1.2 Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive
1.3 Connect to SharePoint On-Premises
For SharePoint 2019, 2016, or 2013:
$Cred = Get-Credential
Connect-PnPOnline -Url "https://yourserver/sites/YourSite" -Credentials $Cred
Result: Successfully connected to SharePoint Online and On-Premises.
Step 2: Automating Site Management
2.1 List All Site Collections
Get-PnPTenantSite | Select Title, Url, StorageUsage, Template
Result: Lists all SharePoint Online sites.
For On-Premises, use:
Get-PnPSite -Includes Url, Title
2.2 Create a New SharePoint Site
New-PnPTenantSite -Url "https://yourtenant.sharepoint.com/sites/ITAdmin" -Title "IT Admin Site" -Owner "admin@yourtenant.com" -TimeZone 13 -Template "STS#3"
Result: Creates a new SharePoint site.
2.3 Delete an Unused SharePoint Site
Remove-PnPTenantSite -Url "https://yourtenant.sharepoint.com/sites/OldSite" -Force
Result: Removes the specified SharePoint site.
Step 3: Managing Users and Permissions
3.1 Retrieve All Users in a Site
Get-PnPUser -Web "https://yourtenant.sharepoint.com/sites/ITAdmin"
Result: Lists all users in the site.
3.2 Add a User to a SharePoint Group
Add-PnPUserToGroup -LoginName "user@yourtenant.com" -Group "IT Admins"
Result: Adds user to the IT Admins group.
3.3 Remove a User from a Group
Remove-PnPUserFromGroup -LoginName "user@yourtenant.com" -Group "IT Admins"
Result: Removes user from IT Admins group.
3.4 Assign Specific Permissions to a User
Set-PnPListItemPermission -List "Documents" -User "user@yourtenant.com" -AddRole "Contribute"
Result: Assigns Contribute permissions to the user.
Step 4: Automating Document Management
4.1 List All Documents in a Library
Get-PnPListItem -List "Documents" | Select Title, FileRef
Result: Retrieves all documents.
4.2 Upload a File to a Library
Add-PnPFile -Path "C:\Reports\Report.pdf" -Folder "Shared Documents"
Result: Uploads Report.pdf to SharePoint.
4.3 Download a File from SharePoint
Get-PnPFile -Url "/sites/ITAdmin/Shared Documents/Report.pdf" -Path "C:\Reports\Report.pdf" -AsFile
Result: Downloads Report.pdf from SharePoint.
4.4 Delete a File
Remove-PnPFile -ServerRelativeUrl "/sites/ITAdmin/Shared Documents/OldReport.pdf" -Force
Result: Deletes OldReport.pdf.
Step 5: Monitoring SharePoint Usage
5.1 Get Site Storage Usage Report
Get-PnPTenantSite | Select Title, Url, StorageUsage, StorageQuota
Result: Displays storage usage of sites.
5.2 Retrieve List of Active Users
Get-PnPUser | Select Title, Email, IsSiteAdmin
Result: Lists active users and admins.
Step 6: Security and Compliance Audits
6.1 Get SharePoint Audit Logs
Search-UnifiedAuditLog -StartDate (Get-Date).AddDays(-7) -EndDate (Get-Date) -RecordType SharePointFileOperation | Select UserIds, Operations, ObjectId
Result: Retrieves audit logs for the last 7 days.
6.2 Find External Users with Access
Get-PnPExternalUser
Result: Lists external users with access.
6.3 Remove External User Access
Remove-PnPExternalUser -Email "externaluser@domain.com"
Result: Revokes access from an external user.
Step 7: Automating Custom Scripts with Task Scheduler
1️⃣ Save the script as ITAdmin_Automation.ps1
2️⃣ Open Task Scheduler in Windows
3️⃣ Create a new task
4️⃣ Set the trigger to Daily
5️⃣ Set the action to Run PowerShell script:
powershell.exe -ExecutionPolicy Bypass -File "C:\Scripts\ITAdmin_Automation.ps1"
Result: Automates IT Admin tasks daily.