How to to get all site collections with their sub webs using PnP PowerShell?

Published May 05 2021 01:32 AM 1,359 Views
Senior Member

Sometimes we have a business requirement to get site collections with all the sub-webs so we can achieve the solution easily using PnP Powershell.

 

Let's see step-by-step implementation:

 

Implementation

 

Open Windows Powershell ISE
Create a new file and write a script

Now we will see all the steps which we required to achieve the solution:

 

1. We will initialize the admin site URL, username, and password in the global variables.

2. Then we will create a Login function to connect the O365 SharePoint Admin site.

3. Create a function to get all site collections and all the sub-webs 

 

So in the end, our script will be like this,

 

 

$SiteURL = "https://domain-admin.sharepoint.com/"
$UserName = "UserName@domain.onmicrosoft.com"
$Password = "********"
$SecureStringPwd = $Password | ConvertTo-SecureString -AsPlainText -Force 
$Creds = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecureStringPwd

Function Login {
    [cmdletbinding()]
    param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $Creds)
    Write-Host "Connecting to Tenant Admin Site '$($SiteURL)'" 
    Connect-PnPOnline -Url $SiteURL -Credentials $creds
    Write-Host "Connection Successfull"
}

Function AllSiteCollAndSubWebs() {
    Login($Creds)
    $TenantSites = (Get-PnPTenantSite) | Select Title, Url       
       
    ForEach ( $TenantSite in $TenantSites) { 
        Connect-PnPOnline -Url $TenantSite.Url -Credentials $Creds
        Write-Host $TenantSite.Title $TenantSite.Url
        $subwebs = Get-PnPSubWebs -Recurse | Select Title, Url
        foreach ($subweb in $subwebs) { 
            Connect-PNPonline -Url $subweb.Url -Credentials $Creds
            Write-Host $subweb.Title $subweb.Url 
        }  
    }
}

AllSiteCollAndSubWebs

 

 

 

Output:

 

AllSCAndSCW.png

 

 

Summary:

 

In this article, we have seen how to get all the site collections and their subwebs using pnp Powershell.

 

Hope this helps! If it is helpful to you then share it with others.

 

Sharing is caring!

%3CLINGO-SUB%20id%3D%22lingo-sub-2322131%22%20slang%3D%22en-US%22%3EHow%20to%20to%20get%20all%20site%20collections%20with%20their%20sub%20webs%20using%20PnP%20PowerShell%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2322131%22%20slang%3D%22en-US%22%3E%3CP%3ESometimes%20we%20have%20a%20business%20requirement%20to%20get%20site%20collections%20with%20all%20the%20sub-webs%20so%20we%20can%20achieve%20the%20solution%20easily%20using%20PnP%20Powershell.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20see%20step-by-step%20implementation%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--469796722%22%20id%3D%22toc-hId--469796722%22%3EImplementation%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3EOpen%20Windows%20Powershell%20ISE%3CBR%20%2F%3ECreate%20a%20new%20file%20and%20write%20a%20script%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3ENow%20we%20will%20see%20all%20the%20steps%20which%20we%20required%20to%20achieve%20the%20solution%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E1.%20We%20will%20initialize%20the%20admin%20site%20URL%2C%20username%2C%20and%20password%20in%20the%20global%20variables.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E2.%20Then%20we%20will%20create%20a%20Login%20function%20to%20connect%20the%20O365%20SharePoint%20Admin%20site.%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E3.%20Create%20a%20function%20to%20get%20all%20site%20collections%20and%20all%20the%20sub-webs%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20in%20the%20end%2C%20our%20script%20will%20be%20like%20this%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24SiteURL%20%3D%20%22https%3A%2F%2Fdomain-admin.sharepoint.com%2F%22%0A%24UserName%20%3D%20%22UserName%40domain.onmicrosoft.com%22%0A%24Password%20%3D%20%22********%22%0A%24SecureStringPwd%20%3D%20%24Password%20%7C%20ConvertTo-SecureString%20-AsPlainText%20-Force%20%0A%24Creds%20%3D%20New-Object%20System.Management.Automation.PSCredential%20-ArgumentList%20%24UserName%2C%20%24SecureStringPwd%0A%0AFunction%20Login%20%7B%0A%20%20%20%20%5Bcmdletbinding()%5D%0A%20%20%20%20param(%5Bparameter(Mandatory%20%3D%20%24true%2C%20ValueFromPipeline%20%3D%20%24true)%5D%20%24Creds)%0A%20%20%20%20Write-Host%20%22Connecting%20to%20Tenant%20Admin%20Site%20'%24(%24SiteURL)'%22%20%0A%20%20%20%20Connect-PnPOnline%20-Url%20%24SiteURL%20-Credentials%20%24creds%0A%20%20%20%20Write-Host%20%22Connection%20Successfull%22%0A%7D%0A%0AFunction%20AllSiteCollAndSubWebs()%20%7B%0A%20%20%20%20Login(%24Creds)%0A%20%20%20%20%24TenantSites%20%3D%20(Get-PnPTenantSite)%20%7C%20Select%20Title%2C%20Url%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20ForEach%20(%20%24TenantSite%20in%20%24TenantSites)%20%7B%20%0A%20%20%20%20%20%20%20%20Connect-PnPOnline%20-Url%20%24TenantSite.Url%20-Credentials%20%24Creds%0A%20%20%20%20%20%20%20%20Write-Host%20%24TenantSite.Title%20%24TenantSite.Url%0A%20%20%20%20%20%20%20%20%24subwebs%20%3D%20Get-PnPSubWebs%20-Recurse%20%7C%20Select%20Title%2C%20Url%0A%20%20%20%20%20%20%20%20foreach%20(%24subweb%20in%20%24subwebs)%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20Connect-PNPonline%20-Url%20%24subweb.Url%20-Credentials%20%24Creds%0A%20%20%20%20%20%20%20%20%20%20%20%20Write-Host%20%24subweb.Title%20%24subweb.Url%20%0A%20%20%20%20%20%20%20%20%7D%20%20%0A%20%20%20%20%7D%0A%7D%0A%0AAllSiteCollAndSubWebs%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2017716111%22%20id%3D%22toc-hId-2017716111%22%3EOutput%3A%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22AllSCAndSCW.png%22%20style%3D%22width%3A%20725px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F278256i620977FA80EA3531%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22AllSCAndSCW.png%22%20alt%3D%22AllSCAndSCW.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-210261648%22%20id%3D%22toc-hId-210261648%22%3ESummary%3A%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%2C%20we%20have%20seen%20how%20to%20get%20all%20the%20site%20collections%20and%20their%20subwebs%20using%20pnp%20Powershell.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20this%20helps!%20If%20it%20is%20helpful%20to%20you%20then%20share%20it%20with%20others.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESharing%20is%20caring!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2322131%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20this%20article%2C%20we%20will%20see%20how%20to%20get%20all%20the%20site%20collections%20with%20their%20subwebs%20recursively%20using%20PNP%20PowerShell.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2322131%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPnP%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎May 04 2021 09:02 PM
Updated by: