Loop through SharePoint SiteCollection structure

0

Script to loop through a specified SiteCollection. Going from SiteCollection (SPSite), to each web object (SPWeb), to all list objects (SPList) and finally all list items (SPListItem).

# Load
SnapIn Add-PSSnapIn Microsoft.SharePoint.PowerShell 

# Casts [Microsoft.SharePoint.Administration.SPWebApplication]$SPWebApplication = $null
[Microsoft.SharePoint.SPSite]$SPSite = $null
[Microsoft.SharePoint.SPWeb]$SPWeb = $null
[Microsoft.SharePoint.SPList]$SPList = $null
[Microsoft.SharePoint.SPListItem]$SPListItem = $null

# Init
[System.String]$url = "https://sharepoint/"

# Get WebApplication by Url
$SPWebApplication = Get-SPWebApplication -Identity $url 

# Get SiteCollections of WebApplication 
$SPSites = $SPWebApplication.Sites 

# Loop through SiteCollections 
ForEach ( $SPSite in $SPSites ) {
	# Get RootWeb
	$SPWebs = $SPSite.AllWebs

	# Loop through all Webs
	ForEach ( $SPWeb in $SPWebs ) {
		Write-Output "$($SPWeb.Url)"
	        # Loop through all Lists
		ForEach ( $SPList in $SPWeb.Lists ) {
			Write-Output "  $($SPList.Title)"
                        # Check if list contains items
			If ( $SPList.Items -ne 0 ) {
				# Loop through all list items
				ForEach ( $SPListItem in $SPList.Items ) {
				}
			}
		}
	}
	$SPWeb.Dispose() 
}
Share.

About Author

Christian Unnerstall

Christian Unnerstall, founder of devshooin.com, enjoys designing and building things that look nice and work well, whether it's websites or services within the interwebs. Have questions - feel free to reach out to him. Enjoy

Leave a Reply