Get all subsites of a subsite using PowerShell

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://”?

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://”, it only shows the Level 3 sites.


To work around this, I used the “AllWebs” property of the SPSite object and filtered the URL’s starting with “Https://”.

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.

12 thoughts on “Get all subsites of a subsite using PowerShell

  1. | ?{$_.url -like "$StartWeb*"} filter not working like this
    | ?{$_.url -like $StartWeb +’*’} use wild card like this it will work


    • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *