Getting a list of all subsites of a particular site (not a site collection) was a little more work than I expected, so here is how I did it.
Letβs say we have the following situation site structure:
What if we want an overview of all sites under “Https://portal.sharepointrelated.com/Projects”?
My first thought was to use the “Webs” property of the SPWeb object. Unfortunately, this only shows the direct subsites for this site. This means that for “Https://portal.sharepointrelated.com/projects”, it only shows the Level 3 sites.
Solution
To work around this, I used the “AllWebs” property of the SPSite object and filtered the URL’s starting with “Https://portal.sharepointrelated.com/projects”.
Here is the code used: (Download .zip file)
param ( [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$StartWeb, [Boolean]$IncludeStartWeb = $true ) Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $subsites = ((Get-SPWeb $StartWeb).Site).allwebs | ?{$_.url -like "$StartWeb*"} foreach($subsite in $subsites) { Write-Host $subsite.url }
As you can see in the source code, I added 2 parameters to the script:
StartWeb: String. This is the starting URL. All subsites under this site will be showed in the result.
IncludeStartWeb: Boolean. When set to $false, the output will not include the URL provided in the StartWeb parameter.
Thanks…very helpful! I needed to get just one level of subsites, and this pointed me in the right direction!
Glad that it could help! π
Thanks for this!
Awesome script. Once I learned how to apply the parameter, it was great. Tks.
thanks you for your script, it was great to start a script to activate a feature for each subsite.
| ?{$_.url -like "$StartWeb*"} filter not working like this
| ?{$_.url -like $StartWeb +’*’} use wild card like this it will work
Thanks
Deva
Hi Download link is not working
Hi,
Thanks, the download link has been fixed!
Regards, Nico
What if we have to get the next level of subsites as well?
Hi,
It should contain all subsites, so subsites of subsites as well π
Hi,
If I need to run it for sharepoint online, what changes should i make in the script?
Thanks
Deepika
Hi,
Thanks for taking the time to comment.
I do not have a SPO script for this.
Your best bet would be to look at the PnP PowerShell to see if they have something similar.
I just needed to make a script that gets the whole structure of a site collection with all subsites recursively, see if it helps you (you need to install PnP powershell commandlets for it to work https://github.com/SharePoint/PnP-PowerShell/wiki/Install-SharePointPnPPowerShellOnline,-PowerShell-5.0-and-Nuget-behind-proxy)
function getSubsites($web) {
$ctx.Load($web)
$ctx.ExecuteQuery()
$ctx.Load($web.Webs)
$ctx.ExecuteQuery()
$subsites=$web.Webs
foreach($subsite in $subsites) {
$ctx.Load($subsite)
$ctx.ExecuteQuery()
Write-Host $subsite.Url
getSubsites($subsite)
}
}
$Error.Clear()
$cred = Get-Credential -UserName YOURUSER.NAME@TENANT.COM -Message “Enter credentials”
Connect-PnPOnline -Url SITEURL -Credentials $cred
$web=Get-PnPWeb
$ctx = $web.Context
getSubsites($web)
Thanks for taking the time to share this!
the download link is not working π
The download link doesn’t work, can you please check it?
Sorry, I just fixed the link.