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:

Get-PnPSubWebs

Get-PnPSubWebs cmdlet

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.

Sub-Subsites

Subsites & Sub-subsites

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
Sub-subsites_for_Specific_Subsite

Sub-subsites for a specific Subsite

 

 

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-Member-Count

Get-Member Count

 

Get-PnPSubWebs -Recurse | select Title, ServerRelativeUrl, Created, Description, Language, WebTemplate
Subsites with more info

Subsites with more information

 

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
SubsitesInfo_ExportToCsv

Subsites info exported to Csv file

 

 

Loading Likes...

Leave a Reply

Enjoying this blog? Spread the word to help others :-)

%d bloggers like this: