How to Get All SharePoint Online Sites using PnP PowerShell?

One of my clients recently asked me to get all the sites presented in the SharePoint Online environment. I tried PnP PowerShell for this. In this tutorial, I will show you how to get all SharePoint Online sites using PnP PowerShell.

Here, I will also show you how to get all the SharePoint team sites, communication sites, SharePoint sites that are not connected to a Microsoft 365 group, etc. The PnP PowerShell script will also display the created date of all the SharePoint Online sites.

If you are new to PnP PowerShell, first install it using the cmdlet below, or check out this article, where I have explained it step by step.

Install-Module -Name PnP.PowerShell -Force -AllowClobber

Get All SharePoint Online Sites using PnP PowerShell

First, let me show you how to retrieve all the SharePoint sites using PnP PowerShell. It will display the Site URL and Created Date.

# Connect to SharePoint Online
$TenantAdminUrl = "https://szg52-admin.sharepoint.com"
Connect-PnPOnline -Url $TenantAdminUrl -Interactive

# Retrieve all sites
$sites = Get-PnPTenantSite

# Output all sites with URL
Write-Output "All SharePoint Online Sites:"
$sites | ForEach-Object {
    Write-host "Site Title:" $_.Title -"Site URL: " $_.URL
}

I executed the above script, and you can see the output in the screenshot below:

Get All SharePoint Online Sites using PnP PowerShell

If you want to export to an Excel file, then you can modify the script like the below:

# Connect to SharePoint Online
$TenantAdminUrl = "https://szg52-admin.sharepoint.com"
Connect-PnPOnline -Url $TenantAdminUrl -Interactive

# Retrieve all sites
$sites = Get-PnPTenantSite

# Prepare data for export
$siteData = $sites | ForEach-Object {
    [PSCustomObject]@{
        Title       = $_.Title
        URL         = $_.Url   
        OwnernName =  $_.OwnerEmail
    }
}

# Export data to Excel
$siteData | Export-Excel -Path "C:\MyNewFolder\SharePointSites.xlsx" -AutoSize -Title "SharePoint Online Sites"

Read Check if a SharePoint Site Exists using PnP PowerShell

Get All SharePoint Online Teams Sites using PnP PowerShell

There will be scenarios where you might just want to get all the team sites from 11SharePoint Online using PnP PowerShell.

Below is a complete PowerShell script to get all the modern team sites connected to a Microsoft 365 group.

# Connect to SharePoint Online
$TenantAdminUrl = "https://szg52-admin.sharepoint.com"
Connect-PnPOnline -Url $TenantAdminUrl -Interactive

# Retrieve all sites
$Sites = Get-PnPTenantSite -Template GROUP#0

Write-Output "Group Connected Team Sites:"
# Output all sites with URL
$Sites | ForEach-Object {
    Write-host "Site Title:" $_.Title -"Site URL: " $_.URL
}

You can see the screenshot below that it displays all the group-connected team sites, as I provided the -Template as GROUP#0.

Get all modern team sites connected to a Microsoft 365 group

Here is a complete script to get all SharePoint Online team sites (including those connected to Microsoft 365 groups) using PnP PowerShell.

# Connect to SharePoint Online
$TenantAdminUrl = "https://szg52-admin.sharepoint.com"
Connect-PnPOnline -Url $TenantAdminUrl -Interactive

# Retrieve all sites
$sites = Get-PnPTenantSite
$teamSites = $sites | Where-Object { $_.GroupId -ne $null }
# Output all sites with URL
Write-Output "All SharePoint Online Sites:"
$teamSites | ForEach-Object {
    Write-host "Site Title:" $_.Title -"Site URL: " $_.URL
}

You can see the output in the screenshot below after I executed the above PowerShell script using VS code.

Get All SharePoint Online Teams Sites using PnP PowerShell

Get All SharePoint Online Communication Sites using PnP PowerShell

It is really easy to get all the communications sites using PnP PowerShell in SharePoint Online. You just need to pass the template in the Get-PnPTenantSite like: Get-PnPTenantSite -Template SITEPAGEPUBLISHING#0

Here is the complete script.

# Connect to SharePoint Online
$TenantAdminUrl = "https://szg52-admin.sharepoint.com"
Connect-PnPOnline -Url $TenantAdminUrl -Interactive

# Retrieve all sites
$communicationSites = Get-PnPTenantSite -Template SITEPAGEPUBLISHING#0

Write-Output "Communication Sites:"
# Output all sites with URL
$communicationSites | ForEach-Object {
    Write-host "Site Title:" $_.Title -"Site URL: " $_.URL
}

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Get All SharePoint Online Communication Sites using PnP PowerShell

I hope you got to know now how to get all the SharePoint Online communication sites using PnP PowerShell.

Conclusion

In this tutorial, I have explained how to get all the SharePoint sites using PnP PowerShell. And also:

  • Get all the modern team sites connected to a Microsoft 365 group using PnP PowerShell
  • Get all the communication sites using PnP PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.