Back to script library
Entra / Microsoft 365 · Teams

Populate Teams directory SharePoint list (PnP)

Populate a SharePoint list with Teams directory data using PnP.PowerShell.

Connect & set up

Run these once per session. All scopes are read-only unless the script makes changes.

Import-Module Pnp.PowerShell

Run it

The main script. Copy it, or download the .ps1 and run it from your console.

$CSVFile = 'C:\temp\ListofTeams.csv'
If (!(Get-Item -Path $CSVFile)) {
Write-Host ("Can't load Teams directory data from {0}" -f $CSVFile)
Break
}
# Get credentials for Pnp SharePoint
$Credentials = Get-Credential
# Define target site where the list is created
$Site = "https://o365maestro.sharepoint.com/sites/Office365forITProsCommunications"
$ListName = "Teams Directory"
Import-Module Pnp.PowerShell
$Connection = Connect-PnPOnline -Url $Site -Credentials $Credentials
# Check if list already exists and if so, remove it
$List = Get-PnPList | Where-Object {$_.DisplayName -eq $ListName}
If ($List) {
Write-Host ("Removing previous version of list {0}" -f $List.Title)
Remove-PnPList -Identity $ListName -Force
}
# Create new list
Write-Host ("Creating new list for {0}" -f $ListName)
New-PnpList -Title $ListName -Template Links -EnableVersioning -Connection $Connection | Out-Null
# Add fields
Add-PnpField -List $ListName -DisplayName 'Team Name' -Internalname TeamName -Type Text -AddToDefaultView | Out-Null
Add-PnpField -List $ListName -DisplayName 'Description' -Internalname Description -Type Text -AddToDefaultView | Out-Null
Add-PnpField -List $ListName -DisplayName 'Owner' -Internalname Owner -Type Text -AddToDefaultView | Out-Null
Add-PnpField -List $ListName -DisplayName 'Owner SMTP Address' -Internalname OwnerSMTP -Type Text -AddToDefaultView | Out-Null
Add-PnpField -List $ListName -DisplayName 'Member count' -Internalname MemberCount -Type Number -AddToDefaultView | Out-Null
Add-PnpField -List $ListName -DisplayName 'External count' -Internalname ExternalCount -Type Number -AddToDefaultView | Out-Null
Add-PnpField -List $ListName -DisplayName 'Access' -Internalname AccessMode -Type Text -AddToDefaultView | Out-Null
# Remove the Notes field inherited from the Links template
Remove-PnPField -List $ListName -Identity Notes -Force
Write-Host ("Populating the {0} list with data extracted from Teams" -f $ListName)
[array]$TeamsData = Import-CSV -Path $CSVFile
[int]$i = 0
ForEach ($Team in $TeamsData) {
$i++
Write-Host ("Adding record for team {0} {1}/{2}" -f $Team.Team, $i, $TeamsData.count)
Add-PnPListItem -List $ListName -Values @{
"URL" = $($Team.Deeplink);
"TeamName" = $($Team.Team);
"Description" = $($Team.Description);
"Owner" = $($Team.Owner);
"OwnerSMTP" = $($Team.OwnerSMTP);
"MemberCount" = $($Team.Members);
"ExternalCount" = $($Team.ExternalGuests);
"AccessMode" = $($Team.Access);
} | Out-Null
}
Write-Host "All done"
Attribution