SharePoint PNP Get Sub Sites in Site Collection

The script below will provide you list of sites in Site collection using SharePoint PNP PowerShell

$Creds = Get-Credential
$SiteUrl = "https://tenant.sharepoint.com/sites/sitecollection"
Connect-PnPOnline -Url $SiteUrl -Credentials $Creds
$Site = Get-PnPSite
$Web = Get-PnPWeb

function GetSubWebs($Web, $Context)
{
    $Global:SiteCount++
    $Context.Load($sweb.Webs)
    $Context.ExecuteQuery()
    foreach($aWeb in $sweb.Webs)
    {
        $aWeb.Url
        if($aWeb.Webs.Count > 0)
        {
            GetSubWebs $aWeb $Context
        }
    }
}

$Context= Get-PnPContext
$webs= $Web.Webs
foreach($sweb in $webs)
{
    $Context.Load($sweb)
    $Context.ExecuteQuery()
    $sweb.Url
    GetSubWebs $sweb $Context
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s