PowerShell to get all Sites and Subsites under Site Collection using PowerShell

PowerShell to get all Sites and Subsites under Site Collection using PowerShell

In this post we will discuss how to write PowerShell script to get all sites and sub sites under a site collection using PowerShell.

You may also read:
Steps to list out All workflows presented in SharePoint 2007 Site Collection and SharePoint 2013

SharePoint 2013 How to Use LINQ With SPMetal

SharePoint 2013 Dynamically Adding Dropdown Using REST API and jQuery

Below is the PowerShell command which will display the result.

$Site=”http://win-pfcp2dgt8di/sites/EnjoySharePoint/”

$Sites=new-object Microsoft.SharePoint.SPSite($Site)

foreach($web in $Sites.Allwebs){

Write-Host($web.Url)

$web.Update()}

$Sites.Dispose()

I have used the PowerShell script in the Windows PowerShell ISE and for this I have just added the below line at the begning:

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

Else you will get the error like below:
Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded.


My whole code and the results looks like below:

PowerShell to get all Sites and Subsites under Site Collection using PowerShell
PowerShell to get all Sites and Subsites under Site Collection using PowerShell

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →