Introduction to PnP PowerShell

Loading

What is PnP PowerShell?

PnP PowerShell (Patterns & Practices PowerShell) is a powerful command-line tool used to manage SharePoint Online, Microsoft 365, and Azure environments. It provides an easy way to automate tasks such as managing sites, lists, users, and security settings without needing to use the SharePoint Admin Center manually.


Key Features of PnP PowerShell

Works with SharePoint Online (SPO) and SharePoint On-Premises (2013/2016/2019)
Easier scripting with simple commands (compared to CSOM/REST API)
Supports Microsoft Teams, Microsoft Graph, and Azure AD
Cross-platform (Windows, macOS, and Linux)
Secure authentication using modern authentication (OAuth, Certificates, etc.)


Step 1: Installing PnP PowerShell

You can install PnP PowerShell using PowerShell (Windows, macOS, Linux).

Install for Current User

Install-Module -Name PnP.PowerShell -Scope CurrentUser

Install for All Users (Requires Admin Privileges)

Install-Module -Name PnP.PowerShell -Scope AllUsers

Update PnP PowerShell

Update-Module -Name PnP.PowerShell

Check Installed Version

Get-Module -Name PnP.PowerShell -ListAvailable

Step 2: Connecting to SharePoint Online

Before using PnP PowerShell, you need to connect to your SharePoint environment.

Connect to SharePoint Online (Interactive Login)

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

Connect with Credentials (Not Recommended for Security Reasons)

$cred = Get-Credential
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Credentials $cred

Connect Using Azure AD App (Certificate Authentication)

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -ClientId "<App_ID>" -Tenant "<Tenant_ID>" -CertificatePath "<Path_To_Cert>"

Disconnect from SharePoint

Disconnect-PnPOnline

Step 3: Basic PnP PowerShell Commands

Once connected, you can perform various SharePoint operations.

1. Site Management

Get SharePoint Site Details

Get-PnPSite

Create a New SharePoint Site

New-PnPSite -Type TeamSite -Title "Test Site" -Url "https://yourtenant.sharepoint.com/sites/TestSite"

Delete a SharePoint Site

Remove-PnPSite -Identity "https://yourtenant.sharepoint.com/sites/TestSite" -Force

2. List and Library Management

Get All Lists in a SharePoint Site

Get-PnPList

Create a New List

New-PnPList -Title "ProjectList" -Template GenericList

Delete a List

Remove-PnPList -Identity "ProjectList" -Force

3. User and Permission Management

Get All Users in a SharePoint Site

Get-PnPUser

Add User to a Group

Add-PnPUserToGroup -LoginName "user@yourtenant.onmicrosoft.com" -Group "Site Members"

Remove User from a Group

Remove-PnPUserFromGroup -LoginName "user@yourtenant.onmicrosoft.com" -Group "Site Members"

Step 4: Automating Tasks with PnP PowerShell

PnP PowerShell allows you to create automation scripts to manage SharePoint Online efficiently.

Example: Export All Lists to a CSV File

$lists = Get-PnPList | Select Title, ItemCount, Created
$lists | Export-Csv -Path "C:\ListDetails.csv" -NoTypeInformation

Example: Bulk Upload Files to SharePoint Document Library

$files = Get-ChildItem "C:\Documents"
foreach ($file in $files) {
Add-PnPFile -Path $file.FullName -Folder "Shared Documents"
}

Step 5: Uninstalling PnP PowerShell

If you no longer need PnP PowerShell, uninstall it using:

Uninstall-Module -Name PnP.PowerShell

Leave a Reply

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