-2

I have tried many things but I only get the list of all sites in the main site.

function LoopThroughSites {

        Param ([string]$url)


        $web = get-SPWeb $url
        $count = $web.Site.Allwebs.Count
        foreach($site in $web.AllWebs)
        {           
        $site.URL
        #LoopThroughSites($site.URL)
        }

        $count
        if ($site.title -eq $null)
        {
        #cls
        }


    }

        Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
        #$username = 'users\AD_Group'
        $initialUrl = "Https://test.site.com"

        LoopThroughSites($initialUrl)

The above code provide only the first level of subsite under root site collection but I want to get all of their subsites recursively.

Any good way to do that using Powershell?

3
  • Yes, that only gets the same results as above but doesn't drill down to the subsites that would be listed. Jun 13 '16 at 23:02
  • What is your goal, to find all site collections & subsites in the Farm or in a particular web app? You can get all Site Collections in a Farm just by using the Get-SPSite cmdlet and passing no parameters; if you want only the Site Collections from a given web app, pipe the Get-SPWebApplication cmdlet with Get-SPSite. Once you have all the SPSite objects you want, you can foreach through them, and use .AllWebs to iterate over their respective subsites (i.e. SPWebs). More info here: sharepoint.stackexchange.com/a/22511/25787 Jun 14 '16 at 4:18
3

Try the below code,

function GetAllWebs($url)
{
    try
    {
        $w = Get-SPWeb $url;
        Write-Host ([String]::Format("Procesing web {0}",$w.Url)) -foregroundcolor Blue
        if($w.Webs.Count -gt 0)
        {
            foreach($web in $w.Webs)
            {
                $web.Url
                GetAllWebs $web.Url;
            }
        }
    }
    catch
    {
        Write-Host ([String]::Format("Error processing web at $url, with Exception: {0}", $_.Exception.Message)) -foregroundcolor Red
    }   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.