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 -AllowClobberGet 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:

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.

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 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:

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:
- Enable Custom Script in SharePoint Online Using PnP PowerShell
- Import Data from Excel to SharePoint List using PnP PowerShell
- Add More Than 5000 Items to a SharePoint Online List using PnP PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.