SharePoint PowerShell Commands: Common Snippets


In the world of SharePoint administration and development, PowerShell is an indispensable tool that simplifies complex operations, enhances efficiency, and automates repetitive tasks. Whether you’re working with SharePoint Online or SharePoint On-Premises, having a PowerShell cheat sheet at your fingertips will help you quickly reference common commands and execute them effectively.

This cheat sheet covers essential PowerShell cmdlets for managing SharePoint sites, lists, libraries, users, permissions, and more. It also includes helpful syntax tips, parameter explanations, and real-world use cases to help you streamline your SharePoint management.


🛡️ Prerequisites: Setting Up PowerShell for SharePoint

Before executing SharePoint PowerShell commands, ensure you have the necessary environment and permissions.

💻 1. Install Required Modules
  • For SharePoint Online, install the management shell:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell
  • For PnP PowerShell (recommended for both Online and On-Premises):
Install-Module -Name PnP.PowerShell
  • For SharePoint On-Premises, use the native module:
Add-PSSnapin Microsoft.SharePoint.PowerShell
🔑 2. Connect to SharePoint
  • SharePoint Online:
# Connect using interactive authentication
Connect-PnPOnline -Url "https://tenant-admin.sharepoint.com" -Interactive
  • SharePoint On-Premises:
# Connect to local farm
Connect-SPFarm -ServiceAccount "domain\admin"

🔥 SharePoint PowerShell Cheat Sheet: Common Cmdlets and Use Cases
🌐 1. Site Collection and Site Management

🔹 Get All Site Collections

Get-SPOSite

Retrieves all site collections in the SharePoint Online tenant.


🔹 Create a New Site Collection

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

Creates a new SharePoint Online site collection with the specified owner and storage quota.


🔹 Remove a Site Collection

Remove-SPOSite -Identity "https://tenant.sharepoint.com/sites/OldSite" -Confirm:$false

Deletes a SharePoint Online site collection without confirmation.


🔹 Set Site Collection Storage Quota

Set-SPOSite -Identity "https://tenant.sharepoint.com/sites/NewSite" -StorageQuota 5000

Modifies the storage quota of a SharePoint Online site.


🔹 Enable or Disable Site Sharing

Set-SPOSite -Identity "https://tenant.sharepoint.com/sites/demo" -SharingCapability Disabled

Disables external sharing on a site.


📁 2. List and Library Management

🔹 Get All Lists in a Site

Get-PnPList

Retrieves all lists and libraries in the current SharePoint site.


🔹 Create a New Document Library

New-PnPList -Title "Project Docs" -Template DocumentLibrary

Creates a new document library named “Project Docs”.


🔹 Add a New List Item

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

Adds a new item to the “Tasks” list with the specified title and assignee.


🔹 Retrieve All Items from a Library

Get-PnPListItem -List "Documents"

Displays all documents from the “Documents” library.


🔹 Delete List Items in Bulk

Get-PnPListItem -List "Tasks" | Remove-PnPListItem -Force

Removes all items from the “Tasks” list.


👥 3. User and Group Management

🔹 Get All Site Users

Get-PnPUser

Retrieves all users in the current SharePoint site.


🔹 Add a User to a Group

Add-PnPUserToGroup -LoginName "user@domain.com" -Group "Team Members"

Adds the specified user to the “Team Members” group.


🔹 Remove a User from a Group

Remove-PnPUserFromGroup -LoginName "user@domain.com" -Group "Team Members"

Removes the specified user from the “Team Members” group.


🔹 Grant Site Permissions to a User

Set-PnPGroupPermissions -Identity "Team Members" -Permissions "Full Control"

Assigns “Full Control” permission to the “Team Members” group.


🔒 4. Permissions Management

🔹 Get All Permissions on a Site

Get-PnPRoleDefinition

Displays all permission levels on the current site.


🔹 Break Inheritance on a Library

Set-PnPList -Identity "Documents" -BreakRoleInheritance -CopyRoleAssignments

Breaks permission inheritance on the “Documents” library and copies existing permissions.


🔹 Grant Read-Only Permissions to a Group

Set-PnPListPermissions -Identity "Documents" -Group "Visitors" -Permissions Read

Grants “Read” permissions to the “Visitors” group on the “Documents” library.


🔹 Restore Inheritance on a List

Set-PnPList -Identity "Documents" -ResetRoleInheritance

Restores inherited permissions on the “Documents” library.


📊 5. Metadata and Reporting

🔹 Export Site Collection Inventory to CSV

Get-SPOSite | Select Url, StorageQuota, StorageUsageCurrent | Export-Csv "C:\Reports\SiteInventory.csv" -NoTypeInformation

Generates a CSV report of all site collections with their storage details.


🔹 Retrieve List Item Metadata

Get-PnPListItem -List "Documents" | Select Title, ID, Created, Modified

Displays metadata (Title, ID, Created, Modified) for all items in the “Documents” library.


🔹 Audit Permissions and Export to CSV

Get-PnPSiteGroup | Select Title, Users | Export-Csv "C:\Reports\Permissions.csv" -NoTypeInformation

Exports site group permissions to a CSV file.


🔥 6. Backup and Restore

🔹 Backup a Site Collection

Backup-SPSite -Identity "http://sp2013/sites/demo" -Path "C:\Backups\demo.bak"

Backs up a SharePoint On-Premises site collection.


🔹 Restore a Site Collection

Restore-SPSite -Identity "http://sp2013/sites/demo" -Path "C:\Backups\demo.bak" -Confirm:$false

Restores a SharePoint On-Premises site collection from a backup.


✅ Best Practices for Using SharePoint PowerShell
  • 🔹 Always use -Confirm:$false cautiously to prevent accidental deletions.
  • 🔹 Use -WhatIf to preview actions before executing destructive commands.
  • 🔹 Include Try-Catch blocks for better error handling.
  • 🔹 Log output to CSV or text files for auditing purposes.
  • 🔹 Use PnP PowerShell over native modules when possible—it offers more flexibility and simplified syntax.

🌐 References

This SharePoint PowerShell cheat sheet serves as a quick reference guide to help you perform essential SharePoint tasks efficiently. By mastering these commands, you’ll be able to automate complex operations, streamline administration, and boost productivity in your SharePoint environment.


AD Groups Authentication Automation Backup Compliance Content Type CSS DocumentSet Flows Google GULP Javascript Levels Limitations Metadata MFA Microsoft Node NodeJs O365 OneDrive Permissions PnP PnPJS Policy Power Automate PowerAutomate PowerShell React ReactJs Rest API Rest Endpoint Send an HTTP Request to SharePoint SharePoint SharePoint Groups 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 *