Skip to main content
This section covers automation for both SharePoint Online and Microsoft Teams. While they are separate services, they share an underlying Microsoft 365 Groups infrastructure and are often managed together.

Prerequisites

You have two module options for SharePoint Online. PnP PowerShell is the more feature-rich choice for site-level operations.
# Option 1: PnP PowerShell (recommended for site management)
Install-Module -Name PnP.PowerShell -Force

# Option 2: SPO Management Shell (tenant-level operations)
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force

Connecting to SharePoint Online

# Connect using PnP PowerShell (interactive browser auth)
Connect-PnPOnline -Url https://yourdomain.sharepoint.com -Interactive

# Connect to a specific site
Connect-PnPOnline -Url https://yourdomain.sharepoint.com/sites/YourSite -Interactive

# Connect using SPO Management Shell (tenant admin)
Connect-SPOService -Url https://yourdomain-admin.sharepoint.com
For the SPO Management Shell, use the -admin URL, not the root site URL. For PnP PowerShell, connect to the specific site you want to manage.

Site management

# List all site collections (SPO Management Shell)
Get-SPOSite -Limit All | Select-Object Url, Title, StorageUsageCurrent

# Get a specific site's details
Get-SPOSite -Identity https://yourdomain.sharepoint.com/sites/YourSite

# Create a new site collection
New-SPOSite -Url https://yourdomain.sharepoint.com/sites/NewSite `
    -Owner admin@yourdomain.com `
    -StorageQuota 1024 `
    -Title "New Site" `
    -Template "STS#3"

# Remove a site collection (moves to recycle bin)
Remove-SPOSite -Identity https://yourdomain.sharepoint.com/sites/OldSite

Permissions management

# Get all users with access to a site (PnP)
Get-PnPSiteUser | Select-Object Title, Email, LoginName

# Add a user to a SharePoint group
Add-SPOUser -Site https://yourdomain.sharepoint.com/sites/YourSite `
    -LoginName user@yourdomain.com `
    -Group "YourSite Members"

# Remove a user from a site
Remove-SPOUser -Site https://yourdomain.sharepoint.com/sites/YourSite `
    -LoginName user@yourdomain.com

# Get external users for a site
Get-SPOExternalUser -SiteUrl https://yourdomain.sharepoint.com/sites/YourSite

Lists and libraries (PnP PowerShell)

# Get all lists in a site
Get-PnPList | Select-Object Title, ItemCount, LastItemModifiedDate

# Get items from a list
Get-PnPListItem -List "Documents" -PageSize 100

# Export list items to CSV
Get-PnPListItem -List "ProjectTracking" | 
    ForEach-Object { $_.FieldValues } | 
    Export-Csv -Path "C:\Reports\ListItems.csv" -NoTypeInformation

Build docs developers (and LLMs) love