Get Hub Sites Details And Export It To CSV
Summary
This sample shows how to get hub sites information and export required details to CSV.
Implementation
- Open Windows PowerShell ISE
- Create a new file
- Write a script as below,
- First, we will connect to Tenant admin site.
- Then we will get hub sites details,
- And then we will export it to CSV.
$adminSiteURL = "https://{domain}-admin.sharepoint.com/"
$username = "USERID"
$password = "********"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
$dateTime = "_{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date)
$basePath = "E:\Contribution\PnP-Scripts\Hubsites\Logs\"
$csvPath = $basePath + "\HubSites" + $dateTime + ".csv"
$global:hubSitesReports = @()
Function Login() {
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $creds)
Write-Host "Connecting to Tenant Admin Site '$($adminSiteURL)'" -f Yellow
Connect-PnPOnline -Url $adminSiteURL -Credentials $creds
Write-Host "Connection Successful!" -f Green
}
Function GetHubSites {
try {
Write-Host "Getting hub sites..." -ForegroundColor Yellow
$pnpHubSites = Get-PnPHubSite | select *
Write-Host "Getting hub sites successfully!" -ForegroundColor Green
foreach ($pnpHubSite in $pnpHubSites) {
$global:hubSitesReports += New-Object PSObject -Property ([ordered]@{
'ID' = $pnpHubSite.ID
'Parent HubSite Id' = $pnpHubSite.ParentHubSiteId
'Site Id' = $pnpHubSite.SiteId
'Site Url' = $pnpHubSite.SiteUrl
'Title' = $pnpHubSite.Title
})
}
}
catch {
Write-Host "Error in getting hub sites information:" $_.Exception.Message -ForegroundColor Red
}
Write-Host "Exporting to CSV..." -ForegroundColor Yellow
$global:hubSitesReports| Export-Csv $csvPath -NoTypeInformation -Append
Write-Host "Exported to CSV successfully!" -ForegroundColor Green
}
Function StartProcessing {
Login($creds);
GetHubSites
}
StartProcessing
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Chandani Prajapati (https://github.com/chandaniprajapati) |
Jasey Waegebaert |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.