PowerShell SharePoint Online: Manage Sites with PnP

PowerShell SharePoint Online: Manage Sites with PnP

PowerShell Tips Editor 4 min read
PowerShell SharePoint Online: Manage Sites with PnP

Provisioning 50 project team sites through the SharePoint Admin Center takes a morning. Provisioning them with PnP PowerShell takes 30 seconds. PowerShell SharePoint Online automation through the PnP PowerShell module gives you cmdlets for every SharePoint operation — creating sites, managing lists and items, uploading files, setting permissions, and bulk administration tasks — all scriptable and repeatable without touching a browser.

Install and Connect PnP PowerShell

PnP PowerShell is a community-maintained module available from the PowerShell Gallery. It supports modern authentication including MFA and app-only certificate auth for unattended scripts.

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

# Connect interactively (browser popup, MFA-compatible)
Connect-PnPOnline -Url "https://contoso.sharepoint.com" -Interactive

# Connect to a specific site collection
Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/ProjectAlpha" -Interactive

# App-only connection (for automation — needs Azure AD app with Sites.FullControl)
Connect-PnPOnline -Url "https://contoso.sharepoint.com" `
    -ClientId "your-app-id" `
    -Tenant "contoso.onmicrosoft.com" `
    -CertificateBase64Encoded $certBase64

Get Site Collections

After connecting to the tenant admin URL, retrieve all site collections in the tenant. This requires admin-level permissions.

# Connect to admin URL first
Connect-PnPOnline -Url "https://contoso-admin.sharepoint.com" -Interactive

# Get all site collections
Get-PnPTenantSite | Select-Object Url, Title, StorageUsageCurrent, Status
Url                                          Title           StorageUsageCurrent  Status
---                                          -----           -------------------  ------
https://contoso.sharepoint.com               Root            1024                 Active
https://contoso.sharepoint.com/sites/Alpha   Project Alpha   256                  Active

Create a New Site

Use New-PnPSite to create modern Team Sites or Communication Sites. The site type determines the template applied.

# Create a Team Site (Microsoft 365 Group-connected)
New-PnPSite -Type TeamSite `
    -Title "Project Beta" `
    -Alias "project-beta" `
    -Description "Site for Project Beta team"

# Create a Communication Site
New-PnPSite -Type CommunicationSite `
    -Title "Company News" `
    -Url "https://contoso.sharepoint.com/sites/CompanyNews"

# Bulk create sites from array
$sites = @(
    @{ Title="Project Alpha"; Alias="proj-alpha" },
    @{ Title="Project Beta";  Alias="proj-beta"  }
)
foreach ($site in $sites) {
    New-PnPSite -Type TeamSite -Title $site.Title -Alias $site.Alias
    Write-Output "Created: $($site.Title)"
}

Work with Lists and Items

Lists are the core data structure in SharePoint. PnP PowerShell provides cmdlets for creating lists, reading items, adding items, and updating or deleting entries.

Connect-PnPOnline -Url "https://contoso.sharepoint.com/sites/ProjectAlpha" -Interactive

# Get all lists in the site
Get-PnPList | Select-Object Title, ItemCount, BaseType

# Get items from a list
Get-PnPListItem -List "Tasks" | Select-Object Id,
    @{ N="Title"; E={ $_.FieldValues["Title"] } },
    @{ N="Status"; E={ $_.FieldValues["Status"] } }

# Add a new item to a list
Add-PnPListItem -List "Tasks" -Values @{
    "Title"       = "Deploy new server"
    "Status"      = "In Progress"
    "AssignedTo"  = "[email protected]"
}

Upload Files to Document Library

Use Add-PnPFile to upload individual files or loop through a directory for bulk uploads. The -Folder parameter specifies the relative server path within the site.

# Upload a single file
Add-PnPFile -Path "C:\Reports\monthly-report.xlsx" `
    -Folder "Shared Documents/Reports"

# Upload all files from a local folder
Get-ChildItem -Path "C:\Reports\" -Filter "*.xlsx" | ForEach-Object {
    Add-PnPFile -Path $_.FullName -Folder "Shared Documents/Reports"
    Write-Output "Uploaded: $($_.Name)"
}

Set Site Permissions

Manage SharePoint permission levels on sites and lists. You can add users to built-in permission groups or grant direct permissions.

# Add a user to the Members group (Edit access)
Add-PnPGroupMember -LoginName "[email protected]" -Identity "Project Alpha Members"

# Add a user to site owners
Add-PnPSiteCollectionAdmin -Owners "[email protected]"

# Grant a specific permission level directly to a user
Set-PnPWebPermission -User "[email protected]" -AddRole "Read"

# Check current site admins
Get-PnPSiteCollectionAdmin | Select-Object Title, Email

Common Errors and Fixes

  • App-only auth requires correct API permissions: For unattended PnP connections using certificate auth, the Azure AD app needs the Sites.FullControl.All application permission in SharePoint (not Graph) AND the app must be granted site collection admin rights with Grant-PnPAzureADAppSitePermission or registered as a tenant app. Missing either causes “Access Denied” even with admin credentials.
  • Throttling on large operations: SharePoint Online throttles API calls when you exceed request limits — typically manifesting as HTTP 429 errors with a retry-after header. For bulk operations, use PnP batch cmdlets: Add-PnPBatch / Submit-PnPBatch reduces round trips significantly. For large file uploads or mass item operations, add Start-Sleep -Milliseconds 500 between iterations or use the PnP batching API.

Related Cmdlets / See Also

Wrapping Up

PnP PowerShell makes SharePoint Online administration scriptable end-to-end — from site provisioning through list management and file uploads. As a next step, build a project kickoff script that creates a new Team Site, pre-populates a task list from a template CSV, and uploads the project charter document — a full provisioning workflow that runs in under a minute.

Send-Item -To