Skip to content

Instantly share code, notes, and snippets.

@OlafD
Created July 31, 2020 08:15
Show Gist options
  • Save OlafD/0c3bbc9c8c89541c1d36c0db9bfebad1 to your computer and use it in GitHub Desktop.
Save OlafD/0c3bbc9c8c89541c1d36c0db9bfebad1 to your computer and use it in GitHub Desktop.
Loop all subwebs of a site collection (root site). Needs the PowerShell PnP extensions for SharePoint Online.
param (
[string]$Url,
$Credentials
)
function LoopWebs
{
param (
$WebCollection
)
foreach ($web in $WebCollection)
{
$url = $web.Url
$subwebConnection = Connect-PnPOnline -Url $url -Credentials $Credentials -ReturnConnection
# do handling for this web
Write-Host $url
$web = Get-PnPWeb -Includes Webs -Connection $subwebConnection
LoopWebs -WebCollection $web.Webs
}
}
if ($Credentials -eq $null)
{
$Credentials = Get-Credential
}
$rootConnection = Connect-PnPOnline -Url $Url -Credentials $Credentials -ReturnConnection
$web = Get-PnPWeb -Includes Webs -Connection $rootConnection
$url = $web.Url
# do handling for this web
Write-Host $url
LoopWebs -WebCollection $web.Webs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment