PowerShell is a powerful scripting language widely used for managing Microsoft 365 environments, including SharePoint Online. The PnP PowerShell (Patterns and Practices PowerShell) module is a community-driven set of cmdlets that simplifies SharePoint Online management.
In this guide, we will cover how to list all site collections in a SharePoint Online tenant using PnP PowerShell. This step-by-step process is useful for administrators who need to manage and audit SharePoint Online environments efficiently.
Prerequisites
Before running the script, ensure you meet the following requirements:
- Microsoft 365 Subscription – You must have a Microsoft 365 tenant with SharePoint Online.
- Global Administrator or SharePoint Admin Role – You need administrative rights to access all site collections.
- PnP PowerShell Module Installed – If not installed, you can install it using PowerShell.
- PowerShell Execution Policy – Ensure that scripts are allowed to run on your system.
Step 1: Install and Import PnP PowerShell Module
First, we need to install the PnP PowerShell module if it is not already installed. Open PowerShell as Administrator and run the following command:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
-Name PnP.PowerShell
: Specifies the module name.-Scope CurrentUser
: Installs it for the current user only.-AllowClobber
: Allows overwriting if an older version exists.-Force
: Forces installation without prompts.
Once installed, import the module using:
Import-Module PnP.PowerShell
This ensures that the cmdlets are available in your PowerShell session.
Step 2: Connect to SharePoint Online
To list all site collections, you need to authenticate and establish a connection with SharePoint Online. Run the following command:
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive
- Replace
yourtenant
with your actual tenant name. -Interactive
allows you to log in via the Microsoft authentication window.
Alternatively, for unattended scripts, use app-based authentication:
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -ClientId "Your-App-Client-ID" -Tenant "yourtenant.onmicrosoft.com" -CertificatePath "Path\To\Certificate.pfx"
This method requires Azure AD App Registration.
Step 3: Retrieve All Site Collections
Now that we are connected, use the following command to fetch all site collections:
$sites = Get-PnPTenantSite
This command retrieves all SharePoint Online site collections, including:
✔ Team sites
✔ Communication sites
✔ Hub sites
✔ Classic sites
To display the list of site collections in a table format:
$sites | Format-Table Url, Title, Template, StorageUsageCurrent -AutoSize
- Url: Displays the site collection’s URL.
- Title: Name of the site.
- Template: Site type (e.g., STS#3 for team site).
- StorageUsageCurrent: Current storage usage in MB.
Step 4: Export Site Collections to a CSV File
If you need to store this information for future reference, export the list to a CSV file using:
$sites | Select-Object Url, Title, Template, StorageUsageCurrent | Export-Csv -Path "C:\SharePointSites.csv" -NoTypeInformation
- Select-Object: Extracts specific properties.
- Export-Csv: Saves the data as a CSV file.
- -Path: Defines where the file is stored.
- -NoTypeInformation: Prevents unnecessary metadata from being added.
To open the file, navigate to the directory and open SharePointSites.csv in Excel.
Step 5: Filtering Site Collections
You might want to filter the results to get specific sites.
1. Get Only Communication Sites
$communicationSites = $sites | Where-Object { $_.Template -eq "SITEPAGEPUBLISHING#0" }
$communicationSites | Format-Table Url, Title
2. Get Sites Created After a Specific Date
$recentSites = $sites | Where-Object { $_.Created -gt (Get-Date).AddDays(-30) }
$recentSites | Format-Table Url, Title, Created
3. Get Sites Exceeding a Storage Limit (e.g., 5000 MB)
$largeSites = $sites | Where-Object { $_.StorageUsageCurrent -gt 5000 }
$largeSites | Format-Table Url, Title, StorageUsageCurrent
Step 6: Disconnect from SharePoint Online
After retrieving the site collections, always disconnect your session to avoid security risks.
Disconnect-PnPOnline
Common Issues & Troubleshooting
- Module Not Found
- Ensure that PnP PowerShell is installed using
Install-Module -Name PnP.PowerShell
.
- Ensure that PnP PowerShell is installed using
- Authentication Fails
- Try using
-Interactive
mode instead of credentials. - Check if your account has the required permissions.
- Try using
- No Sites Found
- Ensure you are connected to the Admin Center URL (
-admin.sharepoint.com
). - Check if your account has access to all sites.
- Ensure you are connected to the Admin Center URL (
- Access Denied
- Run PowerShell as Administrator.
- Ensure your account has SharePoint Admin or Global Admin role.