Find Teams, Channels, Hub Site associated with a SharePoint URL with PnP PowerShell
This post explains how to map a SharePoint site URL back to the corresponding Microsoft Teams team and channel using PnP PowerShell. It also retrieves the Hub Site Url information. The script reads site URLs from a CSV file, connects to each site, and determines whether the site is a teams site or a channel site.
Why this is useful
- Validate which Teams or channels are behind your SharePoint site collection URLs.
- Build inventories that combine SharePoint sites with Teams metadata.
- Populate a SharePoint list, CSV, or reporting table with team and channel names.
What the script does
- Reads
SiteUrlvalues fromSites.csv. - Connects to each SharePoint site using PnP PowerShell.
- Checks if the site has a
GroupIdorRelatedGroupId. - Resolves the Microsoft 365 team display name and channel name.
- Writes the result to a destination SharePoint list.
How the mapping works
- If the site has a non-empty
GroupId, it is treated as a team root site. The script setsChannelNametoGeneral. - If the site has a
RelatedGroupIdbut is not the root team site, the script resolves the Microsoft 365 group display name and derives the channel name from the site title.
Example script
cls
# Connection variables
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$csvPath = Join-Path $directorypath 'Sites.csv' # CSV should have a column 'SiteUrl'
$clientId = 'xxxxxxxxxx'
$domain = 'contoso'
$adminSiteURL = "https://$domain.sharepoint.com"
# Read sites from CSV
$sites = Import-Csv -Path $csvPath
foreach ($s in $sites) {
$siteUrl = $s.SiteUrl
Write-Host "Connecting to site: $siteUrl"
$siteconn = Connect-PnPOnline -Url $siteUrl -ClientId $clientId -ReturnConnection
$site = Get-PnPSite -Includes GroupId, RelatedGroupId, RootWeb.Title, HubSiteId -Connection $siteconn
$TeamName = ''
$ChannelName = ''
$HubSiteId = ''
$HubSiteUrl = ''
if ($site.GroupId -ne [Guid]::Empty) {
# Team root site
$TeamName = $site.RootWeb.Title
$ChannelName = 'General'
$HubSiteId = $siteInfo.HubSiteId
$HubSiteUrl = (HubSiteId.Guid | Get-PnPHubSite).SiteUrl
}
elseif ($site.RelatedGroupId) {
# Channel or connected site
$TeamName = (Get-PnPMicrosoft365Group -Identity $site.RelatedGroupId).DisplayName
$ChannelName = $site.RootWeb.Title -Replace "$TeamName-", ''
$HubSiteId = $siteInfo.HubSiteId
$HubSiteUrl = (HubSiteId.Guid | Get-PnPHubSite).SiteUrl
}
# Add to results array
$results += [PSCustomObject]@{
SiteUrl = $siteUrl
TeamName = $TeamName
ChannelName = $ChannelName
HubSiteId = $HubSiteId
HubSiteUrl = $HubSiteUrl
}
}
$results | Export-Csv -Path ($directorypath + "\TeamsChannelsExport.csv") -NoTypeInformation -Encoding UTF8
Notes
- This example assumes the site title uses the format
TeamName-ChannelNamefor channel sites. - The
Generalchannel is represented by the team root site and does not require a channel-specific title.
Next steps
You can extend this script to:
- save data to a SharePoint list instead of export to CSV
- include owner and member metadata,
- skip unauthorized sites gracefully,
- support alternate auth methods such as certificate auth or device login.