Get Sites, Web and List information from SharePoint Online site using PowerShell

get web sharepoint online powershell

Scenario:
My client wants to know all the site collections, subsites, and lists in my SharePoint Online environment every month. To accumulate this information manually every month is a cumbersome and more possibility for errors.


Solution:
I have written a PowerShell script to pull all this information. The script will get the below information from SharePoint Online:
– Site Collections URL
– Sub Sites URL
– Lists name with hyperlink
– List Item count

I am rendering through all the site collections and subsites in SharePoint online and adding an output to an HTML file. In the HTML file, I am creating hyperlink to all the list/ libraries so that admin/business owner can navigate easily.

PowerShell Script to retrieve site collection url in SharePoint Online:

function Get-SPOSiteInfo {
# Let the user fill in their admin url, username and password
$strSPOAdminUrl = Read-Host “Enter the Admin URL of SPO (eg. https://<Tenant Name>-admin.sharepoint.com)”
$strSPOAdminName = Read-Host “Enter the username of SPO (eg. admin@<tenantName>.onmicrosoft.com)”
$strPwd = Read-Host “Please enter the password for $($strSPOAdminName)” -AsSecureString
# set credentials
$objCredentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $strSPOAdminName, $strPwd
$ObjSPOWeb = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($strSPOAdminName, $strPwd)
#connect to to Office 365
try {
Connect-SPOService -Url $strSPOAdminUrl -Credential $objCredentials
write-host “Connected succesfully to SPO!!!” -foregroundcolor green
}
catch {
write-host “Error: Could not connect to SPO, Contact Administrator !!!” -foregroundcolor red
Break Get-SPOSiteInfo
}
#create HTML file
$strOPFilePath = create-outputfile
#start getting site collections
get-siteCollections
add-content -value ”
</body>
</html>
” -path $strOPFilePath
}

function create-outputfile() {
#Create unique string from the date
$strDate = get-date -format dd_MM_yyyy_hhmm
#set the full path
$strOPFilePath = “$($PSScriptRoot)\SPO-Site-Information$($strDate).html”
#test path
if (!(Test-Path -path $strOPFilePath)) {
#create file
New-Item $strOPFilePath -type file | out-null
#print info
write-host “Output File created: $($strOPFilePath)” -foregroundcolor Black -BackgroundColor Yellow
#add start HTML information to file
add-content -value ”
<html>
<body>
<h1 style=’text-align:center’>SharePoint Online Portal Information</h1>
<table border=’5′ border-style=’solid’ border-collapse=’collapse’ border-color’black’ style=’font-family: Calibri, sans-serif;’>
<tr>
<th style=’background-color:yellow; color:black’>Site Collection/Web URL</th>
<th style=’background-color:yellow; color:black’>Object Type</th>
<th style=’background-color:yellow; color:black’>Site/ Web Template</th>
<th style=’background-color:yellow; color:black’>Item Count</th>
</tr>
” -path $strOPFilePath
}
else {
#break so there won’t be duplicate files
write-host “Output file already exists, wait 1 minute” -foregroundcolor yellow
Break create-outputfile
}
return $strOPFilePath
}
function get-siteCollections {
#Get all site collections
$ObjSiteColls = Get-SPOSite
#Iterate through all site collections
foreach ($ObjSiteColl in $ObjSiteColls) {
#setting variables to align text in HTML File
$strWebpx = 0
$strListpx = 0
#add info to HTML document
add-content -value “<tr style=’background-color:lightblue’><td>$($ObjSiteColl.url)</td><td>Site Collection</td><td>$($ObjSiteColl.template)</td><td></td></tr>” -path $strOPFilePath
write-host “Reading Site Collection: $($ObjSiteColl.url)”
#search for all Subsites for current Site Collection
Get-SPOWebs($ObjSiteColl.url)
}
}

function Get-SPOWebs($url) {
#fill metadata information to the client context variable
$objContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$objContext.Credentials = $ObjSPOWeb
$objWeb = $objContext.Web
$objContext.Load($objWeb)
$objContext.Load($objWeb.Webs)
$objContext.load($objWeb.lists)
try {
$objContext.ExecuteQuery()
#loop through all webs in the web and start again to find more subwebs
$strWebpx = $strWebpx + 10
$strListpx = $strListpx + 15
foreach ($objWeb in $objWeb.Webs) {
add-content -value “<tr style=’background-color: cadetblue;color: white;’><td><span style=’margin-left:$($pixelsweb)px’>$($objWeb.url)</td><td>Web</td><td>$($objWeb.webtemplate)</td><td></td></tr>” -path $strOPFilePath
write-host “Reading Sub-Site: $($objWeb.url)”
Get-SPOWebs($objWeb.url)
}
foreach($objLst in $objWeb.lists)
{
$objContext.Load($objLst.RootFolder)
$objContext.ExecuteQuery()
$objLstUrl= $objWeb.url.Replace($objWeb.ServerRelativeUrl,”)+$objLst.RootFolder.ServerRelativeUrl
Write-Host “List/Library Found::” $($objLst.Title) “`t URL=”$objLstUrl
add-content -value “<tr style=’background-color: white;color: black;’><td><span style=’margin-left:$($strListpx)px’><a href=’$($objLstUrl)’>$($objLst.Title)</a></td><td>List/library</td><td></td><td>$($objLst.itemcount)</td></tr>” -path $strOPFilePath
}
}
catch {
write-host “Could not find web” -foregroundcolor red
}
}
Get-SPOSiteInfo

Output in PowerShell Window:

get sharepoint list information powershell
get sharepoint list information powershell

Out Put in HTML:

get web sharepoint online powershell
get web sharepoint online powershell


Hope this helps, Enjoy the power of PowerShell in SharePoint Online… !!!

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →