SOLVED

Iterating Through Site Collections in a Tenancy using PnP PowerShell

%3CLINGO-SUB%20id%3D%22lingo-sub-64274%22%20slang%3D%22en-US%22%3EIterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-64274%22%20slang%3D%22en-US%22%3E%3CP%3EDoes%20anyone%20know%20how%20I%20can%20Iterate%20through%20a%20series%20of%20Site%20Collections%20in%20a%20Office%20365%20tenancy%20using%20PnP%20PowerShell%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20get%20a%20series%20of%20Site%20Collections%20using%20%3A-%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24SiteCollections%20%3D%20Get-PnPTenantSite%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BNow%20I%20want%20to%20iterate%20through%20%24SIteCollections%20printing%20out%20the%20title%20and%20URL.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307594%22%20slang%3D%22en-US%22%3ERe%3A%20Iterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307594%22%20slang%3D%22en-US%22%3ENever%20mind%2C%20I%20forgot%20about%20-Recurse%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fget-pnpsubwebs%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fget-pnpsubwebs%3Fview%3Dsharepoint-ps%3C%2FA%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-307592%22%20slang%3D%22en-US%22%3ERe%3A%20Iterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-307592%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F2387%22%20target%3D%22_blank%22%3E%40Pieter%20Veenstra%3C%2FA%3E%26nbsp%3BIs%20there%20a%20way%20to%20iterate%20all%20subwebs%20of%20a%20subweb%20using%20PNP%3F%20My%20foreach%26nbsp%3Bloop%20is%20not%20going%20deeper%20than%20the%20initial%20subweb.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat's%20up%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1096%22%20target%3D%22_blank%22%3E%40Dean%20Gross%3C%2FA%3E!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-64330%22%20slang%3D%22en-US%22%3ERe%3A%20Iterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-64330%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F2387%22%20target%3D%22_blank%22%3E%40Pieter%20Veenstra%3C%2FA%3E%20that%20will%20do%20nicely.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-64325%22%20slang%3D%22en-US%22%3ERe%3A%20Iterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-64325%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F4102%22%20target%3D%22_blank%22%3E%40Nigel%20Price%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20go%20for%20something%20like%20this%20to%20list%20all%20subwebs%20for%20each%20site%20collection%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24cred%20%3D%20Get-Credential%0A%0AConnect-PnPOnline%20%22https%3A%2F%2Fmytenant.sharepoint.com%22%20-Credentials%20%24cred%0A%24SiteCollections%20%3D%20Get-PnPTenantSite%0Aforeach%20(%24SiteCollection%20in%20%24SiteCollections)%0A%7B%0A%20%20%20Connect-PnPOnline%20-Url%20%24SiteCollection.Url%20-Credentials%20%24cred%0A%20%20%20Get-PnPSubWebs%0A%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-64305%22%20slang%3D%22en-US%22%3ERe%3A%20Iterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-64305%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Dean%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20this%20work%20with%20PnP%20PowerShell%20or%20does%20this%20work%20with%20CSOM%20only%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-64275%22%20slang%3D%22en-US%22%3ERe%3A%20Iterating%20Through%20Site%20Collections%20in%20a%20Tenancy%20using%20PnP%20PowerShell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-64275%22%20slang%3D%22en-US%22%3E%3CP%3Eyou%20can%20do%20something%20like%3C%2FP%3E%3CP%3E%24sites%20%3D%20Get-SPOSite%20-Limit%20all%20-Detailed%20%7C%20Out-GridView%20-Title%20%22Select%20site%20collections%20%22%20-PassThru%3B%3C%2FP%3E%3CP%3Eforeach(%24site%20in%20%24sites)%3C%2FP%3E%3CP%3E%7B%26nbsp%3Bdo%20stuff%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Regular Contributor

Does anyone know how I can Iterate through a series of Site Collections in a Office 365 tenancy using PnP PowerShell ?

 

I can get a series of Site Collections using :-

 

$SiteCollections = Get-PnPTenantSite

 

 Now I want to iterate through $SIteCollections printing out the title and URL.

6 Replies
Highlighted

you can do something like

$sites = Get-SPOSite -Limit all -Detailed | Out-GridView -Title "Select site collections " -PassThru;

foreach($site in $sites)

{ do stuff

}

 

Highlighted

Hi Dean

 

Does this work with PnP PowerShell or does this work with CSOM only ?

 

Regards

 

Nigel

Highlighted
Solution

Hi @Nigel Price,

 

I would go for something like this to list all subwebs for each site collection:

 

$cred = Get-Credential

Connect-PnPOnline "https://mytenant.sharepoint.com" -Credentials $cred
$SiteCollections = Get-PnPTenantSite
foreach ($SiteCollection in $SiteCollections)
{
   Connect-PnPOnline -Url $SiteCollection.Url -Credentials $cred
   Get-PnPSubWebs
}

 

Highlighted
Highlighted

@Pieter Veenstra Is there a way to iterate all subwebs of a subweb using PNP? My foreach loop is not going deeper than the initial subweb.

 

What's up @Dean Gross!