Site Directory is a site for listing and categorizing important sites in your organization that includes different views for categorized sites, top sites, and a site map. You may be facing with the requirement to provide a site directory of your Office 365 tenant. This can be easily achieved with the PNP PowerShell script below. Before you are running the script make sure that the pre-requisites are met.
Pre-requisites:
- Install PnP PowerShell Currently, the latest build is March 2018 (2.24.1803.0).
You can check the latest module here: https://github.com/SharePoint/PnP-PowerShell/releases - The account running the script must have at least SharePoint Admin role.
Site Directory Script:
$tenantAdministrator = # replace with SharePoint admin role account $tenant = ((New-Object "System.Net.Mail.MailAddress" -ArgumentList $tenantAdministrator).Host).replace(".onmicrosoft.com", '') Write-Host "*******************************************************************************************************************" $tenantPass = Read-Host -Prompt ("$(Get-Date -Format 'yyyy.MM.dd HH:mm:ss') Provide password for {0}" -f $tenantAdministrator) -AsSecureString Write-Host "*******************************************************************************************************************" $tenantURL = "https://{0}.sharepoint.com" -f $tenant $tenantAdminURL = "https://{0}-admin.sharepoint.com" -f $tenant $tenantPNPCredentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $tenantAdministrator, $tenantPass ################################# Connection ###################################### Connect-PnPOnline -Url $tenantAdminURL -Credentials $tenantPNPCredentials $siteDirectory = @() $allSites = Get-PnPTenantSite foreach($site in $allSites) { Write-Host "Connecting to $($site.Url)" -Foregroundcolor Green Connect-PnpOnline -Url $site.Url -Credentials $tenantPNPCredentials -Erroraction SilentlyContinue Write-Host $Error[0].Exception.Message -Foregroundcolor Red $Error.Clear() $rootWeb = Get-PnpWeb -Includes WebTemplate Connect-PnpOnline -Url $site.Url -Credentials $tenantPNPCredentials $rootLists = Get-PnPList -Includes BaseTemplate, ItemCount foreach($list in $rootLists) { $siteDirectory += New-Object PSObject -property @{ SiteCollectionTitle = $site.Title SiteCollectionUrl = $site.Url SiteCollectionTemplate = $site.Template WebTitle = $rootWeb.Title WebUrl = $rootWeb.Url WebTemplate = $rootWeb.WebTemplate ListTitle = $list.Title ListUrl = $list.RootFolder.ServerRelativeUrl ListTemplate = $list.BaseTemplate ListItemCount = $list.ItemCount } Write-Host $site.Title $rootWeb.Title $list.Title -ForegroundColor Yellow } $allWebs = Get-PnPSubWebs -Recurse -Includes WebTemplate foreach($web in $allWebs) { Connect-PnpOnline -Url $web.Url -Credentials $tenantPNPCredentials $allLists = Get-PnpList -Includes BaseTemplate, ItemCount foreach($list in $allLists) { $siteDirectory += New-Object PSObject -property @{ SiteCollectionTitle = $site.Title SiteCollectionUrl = $site.Url SiteCollectionTemplate = $site.Template WebTitle = $web.Title WebUrl = $web.Url WebTemplate = $web.WebTemplate ListTitle = $list.Title ListUrl = $list.RootFolder.ServerRelativeUrl ListTemplate = $list.BaseTemplate ListItemCount = $list.ItemCount } Write-Host $site.Title $web.Title $list.Title } } } $siteDirectory | Select-Object SiteCollectionTitle, SiteCollectionUrl, SiteCollectionTemplate, WebTitle, WebTemplate, WebUrl, ListTitle, ListUrl, ListTemplate, ListItemCount | Export-Csv -NoTypeInformation .\SitesDirectory-$tenant-$(Get-Date -Format "yyyy-MM-dd").csv -Force
The script will generate a CSV file in the same folder as the script, with the following headers and information:
The CSV will contain information about each list item count. Also, it contains the system list – they can be excluded if needed.
Further, Excel Pivot feature can be used to extract information or create beautiful charts.
A list with the list base templates codes can be found here:
A list with web templates codes can be found here:
- GROUP#0 corresponds to Modern Team Sites
- SITEPAGEPUBLISHING#0 corresponds to Communication Sites