Live training: next batches coming soon. Join the waitlist:Waitlists open:Developer Training →Developer →Copilot Studio Training →Copilot Studio →

SharePoint PowerShell Commands: SPO, PnP and Server Cheat Sheet

Most SharePoint admin work comes down to a few dozen cmdlets. The catch is that they live in two different modules, and older guides mix in SharePoint Server commands that never worked in Microsoft 365.

In this tutorial, we’ll go through the SharePoint PowerShell commands I use most, grouped by task. Use PnP PowerShell for sites, lists, files and permissions, and the SPO cmdlets for tenant-wide settings. Here’s the shortest useful start:

Connect-PnPOnline -Url "https://spguides-admin.sharepoint.com" -ClientId "<your-app-id>" -Interactive
Get-PnPTenantSite | Select-Object Title, Url, Template

That signs you in to the admin center and lists every site in the tenant with its URL and template.

SPO cmdlets or PnP PowerShell: which should you use?

The SharePoint Online Management Shell gives you the SPO cmdlets. They cover sites, users, groups and tenant settings, but nothing inside a site: no lists, items or files.

PnP PowerShell covers both levels and runs natively in PowerShell 7. If you don’t have it yet, start with installing PnP PowerShell. Here are the SPO commands next to their PnP equivalents:

TaskSPO cmdletPnP cmdlet
ConnectConnect-SPOServiceConnect-PnPOnline
List sitesGet-SPOSite -Limit AllGet-PnPTenantSite
Create a siteNew-SPOSiteNew-PnPSite, New-PnPTenantSite
Change site settingsSet-SPOSiteSet-PnPTenantSite
Delete a siteRemove-SPOSiteRemove-PnPTenantSite
Restore a deleted siteRestore-SPODeletedSiteRestore-PnPTenantRecycleBinItem
List deleted sitesGet-SPODeletedSite -Limit ALLGet-PnPTenantDeletedSite
Purge a deleted siteRemove-SPODeletedSiteRemove-PnPTenantDeletedSite
Site usersGet-SPOUser -SiteGet-PnPUser
SharePoint groupsGet-SPOSiteGroup -SiteGet-PnPGroup, Get-PnPGroupMember
Add to a SharePoint groupAdd-SPOUserAdd-PnPGroupMember
Site collection adminSet-SPOUser -IsSiteCollectionAdminAdd-PnPSiteCollectionAdmin
Tenant settingsGet-SPOTenant, Set-SPOTenantGet-PnPTenant, Set-PnPTenant
Lists, items, filesNot availableGet-PnPList, Get-PnPListItem, Add-PnPFile, Get-PnPFile
Site recycle binNot availableGet-PnPRecycleBinItem, Restore-PnPRecycleBinItem

Connect to SharePoint Online

Both modules need a one-time install. PnP also needs your own Entra ID app registration for -ClientId, because the shared PnP Management Shell app has been retired:

# PnP PowerShell (PowerShell 7.4+), one-time install
Install-Module PnP.PowerShell -Scope CurrentUser
Connect-PnPOnline -Url "https://spguides.sharepoint.com/sites/SPGuides" -ClientId "<your-app-id>" -Interactive

# SharePoint Online Management Shell (SPO cmdlets)
Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser
Import-Module Microsoft.Online.SharePoint.PowerShell -UseWindowsPowerShell   # only in PowerShell 7
Connect-SPOService -Url "https://spguides-admin.sharepoint.com"

Here’s my connection check against the SPGuides site. It confirms the site title, the module version and how many PnP cmdlets are loaded:

Connect-PnPOnline connecting to a SharePoint Online site and showing the PnP.PowerShell version and cmdlet count

PnP.PowerShell 3.3.0 shipped 899 cmdlets. For certificates, app-only sign-in and the SPO options, see connecting to SharePoint Online using PowerShell.

Site commands

For site collections, the SPO cmdlets are still the classic admin choice. Run them after Connect-SPOService:

# SPO: every site, then one site's storage and sharing
Get-SPOSite -Limit All | Select-Object Title, Url, Template, StorageUsageCurrent
Get-SPOSite -Identity "https://spguides.sharepoint.com/sites/SPGuides" -Detailed

# SPO: create a classic-template site (StorageQuota is in MB)
New-SPOSite -Url "https://spguides.sharepoint.com/sites/Finance" -Owner "admin@spguides.onmicrosoft.com" `
    -StorageQuota 1024 -Title "Finance" -Template "STS#3" -LocaleId 1033

The PnP version runs on an admin center connection. I counted the sites by template and read the sharing settings in one script:

Get-PnPTenantSite counting SharePoint Online sites by template and Get-PnPTenant showing sharing settings

My tenant has 166 sites, and 115 are GROUP#0, which means team sites connected to a Microsoft 365 group. For a full inventory export, see getting all SharePoint Online sites using PnP PowerShell, and for modern sites use New-PnPSite to create a site.

List and list item commands

TaskPnP command
Get all listsGet-PnPList
Create a listNew-PnPList -Title "Tasks" -Template GenericList
Read itemsGet-PnPListItem -List "Tasks" -PageSize 500
Add an itemAdd-PnPListItem -List "Tasks" -Values @{Title="New"}
Update an itemSet-PnPListItem -List "Tasks" -Identity 1 -Values @{Title="Done"}
Delete an itemRemove-PnPListItem -List "Tasks" -Identity 1 -Recycle

Here’s a real run: I created a demo list, added three items, read them back and then listed the biggest lists on the site:

New-PnPList, Add-PnPListItem and Get-PnPListItem creating and reading SharePoint list items

-PageSize matters on big lists. My Service Requests list has 623 items, and paging keeps the read under the 5,000-item view threshold. More examples are in creating, reading, updating and deleting list items with PnP PowerShell.

File and folder commands

Files work the same way: Add-PnPFolder creates a folder, Add-PnPFile uploads, Get-PnPFolderItem lists and Get-PnPFile -AsFile downloads. This run did all four in a new library:

Add-PnPFolder, Add-PnPFile, Get-PnPFolderItem and Get-PnPFile uploading and downloading a SharePoint file

Add-PnPFile returns the uploaded file, so you get its server-relative URL straight away. For bulk work, see uploading files to a SharePoint library using PnP PowerShell and downloading all files from a document library.

User, group and permission commands

Adding people to groups and making someone a site collection admin are the two jobs I script most. Each line pair shows the SPO version first, then PnP:

# Add a user to a SharePoint group
Add-SPOUser -Site "https://spguides.sharepoint.com/sites/SPGuides" -LoginName "alexw@spguides.onmicrosoft.com" -Group "SPGuides Members"
Add-PnPGroupMember -LoginName "alexw@spguides.onmicrosoft.com" -Group "SPGuides Members"

# Make someone a site collection admin
Set-SPOUser -Site "https://spguides.sharepoint.com/sites/SPGuides" -LoginName "meganb@spguides.onmicrosoft.com" -IsSiteCollectionAdmin $true
Add-PnPSiteCollectionAdmin -Owners "meganb@spguides.onmicrosoft.com"

To see who’s in each group, combine Get-PnPGroup with Get-PnPGroupMember:

Get-PnPGroup and Get-PnPGroupMember listing SharePoint groups with member counts

The site has its default Owners, Members and Visitors groups, and 18 users in the site user list. For a full permissions report, see checking site permissions in SharePoint Online.

Sharing and tenant settings commands

External sharing is set at the tenant level and can be tightened per site, never loosened past the tenant. The earlier tenant report showed ExternalUserAndGuestSharing with Anyone links that expire in 30 days. These lines lock it down:

# Tenant level: new and existing guests, no Anyone links
Set-SPOTenant -SharingCapability ExistingExternalUserSharingOnly
Set-PnPTenant -SharingCapability ExistingExternalUserSharingOnly

# One site: turn external sharing off
Set-SPOSite -Identity "https://spguides.sharepoint.com/sites/Finance" -SharingCapability Disabled
Set-PnPTenantSite -Identity "https://spguides.sharepoint.com/sites/Finance" -SharingCapability Disabled

The four values are Disabled, ExistingExternalUserSharingOnly, ExternalUserSharingOnly and ExternalUserAndGuestSharing. To share one site with a partner instead, follow sharing a SharePoint site with external users.

Recycle bin commands

Deleted lists, files and items go to the site recycle bin first. Remove-PnPList -Recycle sends a list there instead of deleting it for good, which is how I cleaned up my demo lists:

Remove-PnPList with -Recycle and Get-PnPRecycleBinItem showing deleted lists in the first-stage recycle bin

Use the Id from that output to restore an item. Deleted sites are different: they sit in the admin center for 93 days, so restore them with the admin commands:

# Site recycle bin: restore one item by its Id
Restore-PnPRecycleBinItem -Identity "6a6e0a0f-e73f-43db-bfe3-f49032d534c1" -Force

# Deleted sites (admin connection)
Get-SPODeletedSite
Restore-SPODeletedSite -Identity "https://spguides.sharepoint.com/sites/Finance"
Restore-PnPTenantRecycleBinItem -Url "https://spguides.sharepoint.com/sites/Finance" -Wait

The SharePoint recycle bin tutorial explains the first and second stages in the browser.

SharePoint Server (on-premises) commands

If you run SharePoint Server, the cmdlets are different. They start with SP and run on a farm server in Windows PowerShell, not PowerShell 7:

# SharePoint Server 2016 / 2019 (Windows PowerShell on a farm server)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Get-SPSite -Limit All | Select-Object Url, Owner
Get-SPWeb "https://intranet.spguides.local/sites/hr" | Select-Object Title, Url
Backup-SPSite "https://intranet.spguides.local/sites/hr" -Path "D:\Backups\hr.bak"
Restore-SPSite "https://intranet.spguides.local/sites/hr" -Path "D:\Backups\hr.bak" -Force

In SharePoint Server Subscription Edition, the cmdlets became a module that loads in any Windows PowerShell console, so you can skip Add-PSSnapin. None of these work against SharePoint Online.

Common errors with SharePoint PowerShell commands

  • The term ‘Get-SPSite’ is not recognized: that’s a SharePoint Server cmdlet. For Microsoft 365, use Get-SPOSite or Get-PnPTenantSite.
  • SPO cmdlets fail in PowerShell 7: import the module with -UseWindowsPowerShell, or run them in Windows PowerShell 5.1.
  • Connect-PnPOnline asks for a client ID: register your own Entra ID app and pass -ClientId.
  • New-SPOSite says the URL is in use: a deleted site with that URL is still in the recycle bin. Restore it or remove it from the admin center first.

Ready to automate more? These guides go deeper on the commands above:

Frequently Asked Questions

What are the most used SharePoint Online PowerShell commands?

Connect-SPOService or Connect-PnPOnline to sign in, Get-SPOSite or Get-PnPTenantSite to list sites, Get-PnPList and Get-PnPListItem for lists, Add-PnPFile for uploads, Add-SPOUser or Add-PnPGroupMember for groups, and Set-SPOTenant for sharing.

What is the difference between SPO cmdlets and PnP PowerShell?

SPO cmdlets come from Microsoft’s SharePoint Online Management Shell and manage sites, users and tenant settings. PnP PowerShell is an open-source module that also works inside sites, with lists, items, files and pages.

How do I install the SharePoint Online Management Shell commands?

Run Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser, then Connect-SPOService with your admin center URL. Microsoft’s SharePoint Online Management Shell guide covers the install and MFA sign-in.

Can I run SharePoint PowerShell commands in PowerShell 7?

Yes. PnP PowerShell runs natively in PowerShell 7.4 or later. The SPO module works in PowerShell 7 when you import it with -UseWindowsPowerShell. SharePoint Server cmdlets need Windows PowerShell.

Where can I find the full list of PnP PowerShell commands?

Run Get-Command -Module PnP.PowerShell to list every cmdlet you have installed; version 3.3.0 has 899. The PnP PowerShell cmdlet reference documents each one with examples.