SharePoint PowerShell Commands: Common Snippets

A reference set of PowerShell cmdlets for day-to-day SharePoint administration — site/list management, users and permissions, metadata reporting, and on-prem backup/restore. Covers both PnP PowerShell (recommended for Online and on-prem alike) and the native SharePoint Online module.


In this post: Setup and connecting · Site collection and site management · List and library management · User and group management · Permissions management · Metadata and reporting · Backup and restore (on-premises) · Best practices · Related reading


Setup and connecting

Install the modules you need:

# SharePoint Online management shell
Install-Module -Name Microsoft.Online.SharePoint.PowerShell

# PnP PowerShell (recommended for both Online and On-Premises)
Install-Module -Name PnP.PowerShell

Connect to SharePoint Online:

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

On-premises is different from Online in one important way worth correcting directly: there’s no “connect” cmdlet at all. Installing the Microsoft.SharePoint.PowerShell snap-in and opening the SharePoint Management Shell on a server already joined to the farm is what gives you access — cmdlets like Get-SPFarm or Get-SPSite then operate on the local farm directly, with no separate connection step:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Get-SPFarm

Site collection and site management
# Get all site collections in the tenant
Get-SPOSite

# Create a new site collection
New-SPOSite -Url "https://tenant.sharepoint.com/sites/NewSite" `
    -Owner "admin@tenant.onmicrosoft.com" -StorageQuota 2048 -Template "STS#3"

# Remove a site collection (no confirmation prompt)
Remove-SPOSite -Identity "https://tenant.sharepoint.com/sites/OldSite" -Confirm:$false

# Adjust storage quota (in MB)
Set-SPOSite -Identity "https://tenant.sharepoint.com/sites/NewSite" -StorageQuota 5000

# Disable external sharing on a specific site
Set-SPOSite -Identity "https://tenant.sharepoint.com/sites/demo" -SharingCapability Disabled

List and library management
# All lists and libraries in the current site
Get-PnPList

# Create a new document library
New-PnPList -Title "Project Docs" -Template DocumentLibrary

# Add a list item
Add-PnPListItem -List "Tasks" -Values @{"Title" = "New Task"; "AssignedTo" = "user@domain.com"}

# Retrieve all items from a library
Get-PnPListItem -List "Documents"

# Delete every item in a list -- use with real caution, see Best Practices below
Get-PnPListItem -List "Tasks" | Remove-PnPListItem -Force

User and group management
# All users on the current site
Get-PnPUser

# Add a user to a group
Add-PnPUserToGroup -LoginName "user@domain.com" -Group "Team Members"

# Remove a user from a group
Remove-PnPUserFromGroup -LoginName "user@domain.com" -Group "Team Members"

# Grant a permission level to a group
Set-PnPGroupPermissions -Identity "Team Members" -AddRole "Full Control"

Permissions management
# All permission levels defined on the current site
Get-PnPRoleDefinition

# Break inheritance on a library, keeping existing permissions as the starting point
Set-PnPList -Identity "Documents" -BreakRoleInheritance -CopyRoleAssignments

# Grant Read to a group on a specific library
Set-PnPListPermission -Identity "Documents" -Group "Visitors" -AddRole "Read"

# Restore inheritance (undo a break)
Set-PnPList -Identity "Documents" -ResetRoleInheritance

On-premises has no “connect” cmdlet at all — opening the SharePoint Management Shell on a server already joined to the farm is what gives you access.

Metadata and reporting
# Export a site inventory with storage details to CSV
Get-SPOSite | Select Url, StorageQuota, StorageUsageCurrent | Export-Csv "C:\Reports\SiteInventory.csv" -NoTypeInformation

# Pull key metadata for every item in a library
Get-PnPListItem -List "Documents" | Select Title, ID, Created, Modified

# Export site group membership for an access audit
Get-PnPSiteGroup | Select Title, Users | Export-Csv "C:\Reports\Permissions.csv" -NoTypeInformation

Backup and restore (on-premises)
# Back up a site collection
Backup-SPSite -Identity "http://sp2013/sites/demo" -Path "C:\Backups\demo.bak"

# Restore it (no confirmation prompt)
Restore-SPSite -Identity "http://sp2013/sites/demo" -Path "C:\Backups\demo.bak" -Confirm:$false

These are on-premises-only — SharePoint Online doesn’t expose an equivalent site-collection-level backup cmdlet; retention policies and the second-stage recycle bin cover that role instead.


Best practices
  • Use -WhatIf before a destructive command where the cmdlet supports it, to preview what would happen without actually doing it.
  • Don’t reach for -Confirm:$false by default — it exists to skip a safety prompt, which is exactly the prompt you want on a bulk delete or site removal until you’re certain of the scope.
  • Wrap scripted operations in try/catch so a failure partway through a bulk operation doesn’t fail silently.
  • Log output to CSV for anything you might need to audit or prove happened later.
  • Default to PnP PowerShell over the native modules where both cover the same operation — generally more consistent syntax and works across both Online and on-prem.

References: Microsoft SharePoint PowerShell docs, PnP PowerShell documentation, PowerShell Gallery.



Bookmark this one — it covers most of what comes up in day-to-day SharePoint admin work. Missing a common snippet? Add it in the comments.


App Catalog Authentication Automation Backup Compliance Content Type CSS Flows Google Javascript Limitations List Metadata MFA Microsoft Node NodeJs O365 OneDrive Permissions PnP PnPJS Policy PowerApps Power Automate PowerAutomate PowerPlatform PowerShell React ReactJs Rest API Rest Endpoint Security Send an HTTP Request to SharePoint SharePoint SharePoint List SharePoint Modern SharePoint Online SPFX SPO Sync Tags Teams Termstore Versioning

Leave a Comment

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