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
}