Retrieve all subsites and sub-subsites in SharePoint Online using PowerShell PnP
Short blog post for today is about getting all your subsites & sub-subsites for a specific Site Collection within your SharePoint Online environment.
As an admin for your Office 365 tenant, you may want to retrieve this information to pass on to an Executive in your company, or maybe simply create a report?
And if so, you should already be familiar with SharePoint PowerShell PnP? If not, you should get right into it!
For more info about the Practices & Patterns (PnP), refer to the official documentation, and/or download the modules from hard workers on GitHub – SharePoint PowerShell PnP.
With that being said, let’s dive in.
Connect to the Site Collection
First things first, let’s connect to the Site Collection we are interested in, by using the Connect-PnPOnline cmdlet.
$cred = Get-Credential
Connect-PnPOnline -Url https://<YOUR_TENANT_NAME>.sharepoint.com/sites/pnptest -Credentials $cred
Retrieve the subsites
Once connected, we’ll use the Get-PnPSubWebs cmdlet.
So let’s run Get-PnPSubWebs and check the results:
As you can see, by default we have the Title, ServerRelativeUrl, and the ID.
Those are the subsites of the Site Collection we are connected to.
Retrieve the sub-subsites
We can also retrieve the sub-subsites by using the -Recurse parameter.
Above, circled in red are the sub-subsites of “Subsite A” and “Subsite B”
Pretty easy isn’t it? 🙂
Retrieve sub-subsites for a specific subsite
Let’s narrow a bit, and retrieve only the sub-subsites for one subsite (i.e.: Subsite B)
Get-PnPSubWebs -Web "/sites/pnptest/subsiteb" -Recurse
Retrieve more info & Export to CSV file
In case you need to get specific information, pipe the cmdlet to Get-Member, and choose amongst more than 170 methods and properties! (at the time of writing: March 2018)
Get-PnPSubWebs -Recurse | select Title, ServerRelativeUrl, Created, Description, Language, WebTemplate
And finally, pipe to Export-Csv and get your report! 🙂
Get-PnPSubWebs -Recurse | select Title, ServerRelativeUrl, Created, Description, Language, WebTemplate | Export-Csv C:\Users\$env:USERNAME\Desktop\PnPSubsitesInfo.csv -NoTypeInformation