How to Retrieve Basic SharePoint Web Information – PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   04 Jun 2016
Visit Count
Today :  2    Total :   3577



In this article, let us see, how to retrieve the basic information of a Web in a SharePoint Site.

 ######################### this method will return the basic information about the Web
 
 function GetWebInfo()
 
 {
 
 Write-Host "Entered GetWebInfo Method" -ForegroundColor Yellow 
 
 Add-Content "$ProgressFile" "Entered GetWebInfo Method"
 
 $script:Progress = "7:Entered"
 
 # Get Web Application Object
 
 $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
 # Assign the XML Output File Paths
 
 $WebInfo_XML_Path = $scriptBase + "\Reports-$LogTime\WebInfo.xml"
 
 # Create the XML File Tags
 
 $xmlWriter = New-Object System.XMl.XmlTextWriter($WebInfo_XML_Path,$Null)
 
 $xmlWriter.Formatting = 'Indented'
 
 $xmlWriter.Indentation = 1
 
 $XmlWriter.IndentChar = "`t"
 
 $xmlWriter.WriteStartDocument()
 
 $xmlWriter.WriteComment('Web Information' + $WebApplication.DisplayName)
 
 $xmlWriter.WriteStartElement('WebApplication')
 
 $xmlWriter.WriteEndElement()
 
 $xmlWriter.WriteEndDocument()
 
 $xmlWriter.Flush()
 
 $xmlWriter.Close()
 
 # Get All the Site Collections
 
 $SiteCollections = $WebApplication | Get-SPSite -Limit All
 
 #$SiteCollections = Get-SPSite "http://ctsc00556722901:5555/sites/TeamSite2/"
 
 # write the output on XML File
 
 $xmlDoc = [System.Xml.XmlDocument](Get-Content $WebInfo_XML_Path);
 
 $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
 
 $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
 
 $xmlDoc.Save($WebInfo_XML_Path)
 
 # Iterate through the SiteCollections
 
 foreach($SiteCollection in $SiteCollections)
 
 {
 
 $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
 
 # Get the SubWebs inside the Site Collection
 
 $Webs = Get-SPWeb -site $SiteCollection -Limit All 
 
 # write the output on XML File
 
 $xmlDoc = [System.Xml.XmlDocument](Get-Content $WebInfo_XML_Path);
 
 $siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
 
 $xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
 
 $siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
 
 $contentDBNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("ContentDB"));
 
 $contentDBNode.SetAttribute("Name", $SiteCollection.ContentDatabase.Name)
 
 $AdministratorsNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SiteCollectionAdministrators"));
 
 # Get the Site Collection Administrators and Iterate
 
 foreach($siteAdmin in $SiteCollection.RootWeb.SiteAdministrators)
 
 {
 
 $AdministratorElement = $AdministratorsNode.AppendChild($xmlDoc.CreateElement("Administrator"));
 
 $AdministratorElement.SetAttribute("DisplayName", $siteAdmin.Name)
 
 $AdministratorElement.SetAttribute("LoginName", $siteAdmin.UserLogin)
 
 }
 
 # Check for Publishing Site
 
 $IsPublishingNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("IsPublishing"));
 
 if ([Microsoft.SharePoint.Publishing.PublishingSite]::IsPublishingSite($SiteCollection))
 
 {
 
 $IsPublishingTextNode = $IsPublishingNode.AppendChild($xmlDoc.CreateTextNode("True"));
 
 }
 
 Else
 
 {
 
 $IsPublishingTextNode = $IsPublishingNode.AppendChild($xmlDoc.CreateTextNode("False"));
 
 }
 
 $subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SubSites"));
 
 $subSitesNode.SetAttribute("Count", $Webs.Count)
 
 $xmlDoc.Save($WebInfo_XML_Path)
 
 # Iterate through the Webs
 
 foreach($Web in $Webs)
 
 {
 
 # write the output on XML File
 
 $subSiteNameNode = $subSitesNode.AppendChild($xmlDoc.CreateElement("SubSite"));
 
 if($Config.Configuration.WebInfo.Title -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $NameElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Title"));
 
 $NameElementTextNode = $NameElement.AppendChild($xmlDoc.CreateTextNode($Web.Title));
 
 }
 
 if($Config.Configuration.WebInfo.WebID -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $WebIDElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("WebID"));
 
 $WebIDElementTextNode = $WebIDElement.AppendChild($xmlDoc.CreateTextNode($Web.Id));
 
 }
 
 if($Config.Configuration.WebInfo.ParentWebID -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $ParentWebIDElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("ParentWebID"));
 
 $ParentWebIDElementTextNode = $ParentWebIDElement.AppendChild($xmlDoc.CreateTextNode($Web.ParentWebID));
 
 }
 
 if($Config.Configuration.WebInfo.ParentWebTitle -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $parentWebTitle = ""
 
 if($Web.ParentWebID -ne "00000000-0000-0000-0000-000000000000")
 
 {
 
 $parentWeb = $SiteCollection.OpenWeb($Web.ParentWebID)
 
 $parentWebTitle = $parentWeb.Title
 
 }
 
 else
 
 {
 
 $parentWebTitle = "RootWeb"
 
 } 
 
 $ParentWebTitleElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("ParentWebTitle"));
 
 $ParentWebTitleElementTextNode = $ParentWebTitleElement.AppendChild($xmlDoc.CreateTextNode($parentWebTitle));
 
 }
 
 if($Config.Configuration.WebInfo.Description -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $DescriptionElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Description"));
 
 $DescriptionElementTextNode = $DescriptionElement.AppendChild($xmlDoc.CreateTextNode($Web.Description));
 
 }
 
 if($Config.Configuration.WebInfo.Url -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $UrlElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Url"));
 
 $UrlElementTextNode = $UrlElement.AppendChild($xmlDoc.CreateTextNode($Web.Url));
 
 }
 
 if($Config.Configuration.WebInfo.Size -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $webSize = GetWebSize $Web
 
 $SizeElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Size"));
 
 $SizeElementTextNode = $SizeElement.AppendChild($xmlDoc.CreateTextNode([string]$webSize + " MB"));
 
 }
 
 if($Config.Configuration.WebInfo.SiteCreationDate -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $SiteCreationDateElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("SiteCreationDate"));
 
 $SiteCreationDateElementTextNode = $SiteCreationDateElement.AppendChild($xmlDoc.CreateTextNode($Web.Created));
 
 }
 
 if($Config.Configuration.WebInfo.LastModifiedDate -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $LastModifiedDateElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("LastModifiedDate"));
 
 $LastModifiedDateElementTextNode = $LastModifiedDateElement.AppendChild($xmlDoc.CreateTextNode($Web.LastItemModifiedDate));
 
 }
 
 if($Config.Configuration.WebInfo.DayWebExist -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $DayWebExistElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("DayWebExist"));
 
 $Today_date = Get-Date
 
 if ($Today_date -ge $Web.Created)
 
 {
 
 $diff = ($Today_date- $Web.Created).Days
 
 } 
 
 $DayWebExistElementTextNode = $DayWebExistElement.AppendChild($xmlDoc.CreateTextNode($diff));
 
 }
 
 if($Config.Configuration.WebInfo.SiteCollection -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $SiteCollectionElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("SiteCollection"));
 
 $SiteCollectionElement.SetAttribute("Name", $siteCollectionName.Title)
 
 $SiteCollectionElement.SetAttribute("URL", $SiteCollection.Url)
 
 }
 
 if($Config.Configuration.WebInfo.Language -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $webLanguage = GetWebLanguage $Web
 
 $LanguageElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Language"));
 
 $LanguageElementTextNode = $LanguageElement.AppendChild($xmlDoc.CreateTextNode([string]$webLanguage));
 
 }
 
 if($Config.Configuration.WebInfo.IsRootWeb -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $IsRootWebElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("IsRootWeb"));
 
 $IsRootWebElementTextNode = $IsRootWebElement.AppendChild($xmlDoc.CreateTextNode($Web.IsRootWeb));
 
 }
 
 if($Config.Configuration.WebInfo.SubWebCount -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $SubWebCountElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("SubWebCount"));
 
 $SubWebCountElementTextNode = $SubWebCountElement.AppendChild($xmlDoc.CreateTextNode($Web.GetSubwebsForCurrentUser().Count));
 
 }
 
 if($Config.Configuration.WebInfo.IsPublishing -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $IsPublishingElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("IsPublishing"));
 
 $IsPublishingElementTextNode = $IsPublishingElement.AppendChild($xmlDoc.CreateTextNode([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web)));
 
 if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($Web))
 
 {
 
 $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
 
 $pageLayouts = $publishingWeb.GetAvailablePageLayouts()
 
 $PageLayoutsElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("PageLayouts"));
 
 $PageLayoutsElement.SetAttribute("Count", $pageLayouts.Count)
 
 foreach($pageLayout in $pageLayouts)
 
 {
 
 $PageLayoutElement = $PageLayoutsElement.AppendChild($xmlDoc.CreateElement("PageLayout"));
 
 $PageLayoutElement.SetAttribute("Name", $pageLayout.Name)
 
 $PageLayoutElement.SetAttribute("Title", $pageLayout.Title)
 
 $PageLayoutElement.SetAttribute("ServerRelativeUrl", $pageLayout.ServerRelativeUrl)
 
 }
 
 }
 
 }
 
 if($Config.Configuration.WebInfo.WebTemplate -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $WebTemplateElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("WebTemplate"));
 
 $WebTemplateElementTextNode = $WebTemplateElement.AppendChild($xmlDoc.CreateTextNode($Web.WebTemplate));
 
 }
 
 if($Config.Configuration.WebInfo.EventReceivers -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $EventReceiversElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("EventReceivers"));
 
 $EventReceiversElement.SetAttribute("Count", $SPweb.EventReceivers.Count)
 
 if($SPweb.EventReceivers.Count -gt 0)
 
 {
 
 foreach($E_Rec in $SPweb.EventReceivers)
 
 {
 
 $EventReceiverElement = $EventReceiversElement.AppendChild($xmlDoc.CreateElement("EventReceiver"));
 
 $EventReceiverElement.SetAttribute("Name", $E_Rec.Name)
 
 }
 
 }
 
 }
 
 if($Config.Configuration.WebInfo.Roles -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $RolesElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Roles"));
 
 $RolesElement.SetAttribute("Count", $Web.Roles.Count)
 
 if($Web.Roles.Count -gt 0)
 
 {
 
 foreach($Role in $Web.Roles)
 
 {
 
 $RoleElement = $RolesElement.AppendChild($xmlDoc.CreateElement("Role"));
 
 $RoleElement.SetAttribute("Name", $Role.Name)
 
 $RoleElement.SetAttribute("Type", $Role.Type)
 
 $RoleElement.SetAttribute("Description", $Role.Description)
 
 }
 
 }
 
 }
 
 if($Config.Configuration.WebInfo.Groups -eq "True" -or $Config.Configuration.WebInfo.FetchAll -eq "True")
 
 {
 
 $GroupsElement = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Groups"));
 
 $GroupsElement.SetAttribute("Count", $Web.Groups.Count)
 
 if($Web.Groups.Count -gt 0)
 
 {
 
 foreach($Group in $Web.Groups)
 
 {
 
 $GroupElement = $GroupsElement.AppendChild($xmlDoc.CreateElement("Group"));
 
 $GroupElement.SetAttribute("Name", $Group.Name)
 
 $GroupElement.SetAttribute("UsersCount", $Group.Users.Count)
 
 if($Group.Users.Count -gt 0)
 
 {
 
 $UsersElement = $GroupElement.AppendChild($xmlDoc.CreateElement("Users"));
 
 foreach($User in $Group.Users)
 
 {
 
 $UserElement = $UsersElement.AppendChild($xmlDoc.CreateElement("User"));
 
 $UserElement.SetAttribute("DisplayName", $User.Name)
 
 $UserElement.SetAttribute("LoginName", $User.UserLogin)
 
 }
 
 }
 
 }
 
 }
 
 }
 
 $xmlDoc.Save($WebInfo_XML_Path)
 
 }
 
 }
 
 # write the output on XML File
 
 $xmlDoc.Save($WebInfo_XML_Path)
 
 Write-Host "Completed GetWebInfo Method" -ForegroundColor Yellow 
 
 Add-Content "$ProgressFile" "Completed GetWebInfo Method"
 
 $script:Progress = "7:Success"
 
 }
 
 # Recursive Method to identify the Folder Size
 
 function GetSPFolderSize ($Folder)
 
 {
 
 $byteCount = 0
 
 # calculate the files in the immediate folder
 
 foreach ($SPFile in $Folder.Files)
 
 {
 
 $byteCount += $SPFile.TotalLength 
 
 # also include file versions
 
 foreach ($SPFileVersion in $SPFile.Versions)
 
 {
 
 $byteCount += $SPFileVersion.Size
 
 }
 
 # Calculating number of versions inside a List
 
 }
 
 # Handle sub folders
 
 foreach ($SubFolder in $Folder.SubFolders)
 
 {
 
 $byteCount += GetSPFolderSize $SubFolder
 
 }
 
 return $byteCount
 
 }
 
 # Get the Web Size
 
 function GetWebSize($Web)
 
 {
 
 $lRecyclesize = $null
 
 $total = $null
 
 $SPRecycleBinItemCollection = $Web.RecycleBin;
 
 foreach ( $oItem in $SPRecycleBinItemCollection)
 
 {
 
 $lRecyclesize += $oItem.Size;
 
 }
 
 # Calling the function to Calculate file size inside Root folder
 
 try
 
 {
 
 $webBytes = GetSPFolderSize $Web.RootFolder
 
 $webBytes += $lRecyclesize;
 
 [double]$total += $webBytes
 
 #Converting the byte size to MB
 
 $total = $total / 1048576
 
 $total = "{0:N3}" -f $total
 
 }
 
 catch
 
 {
 
 $Error_C4 = $SPWeb.Url+" : "+$_.exception.Message+" - "+ "Exception In File Size calculation"
 
 $TimeStamp = Get-Date 
 
 [String]$total = "Size Cannot be Calculated"
 
 }
 
 return $total
 
 }
 
 # Get the Web Langauge 
 
 function GetWebLanguage($SPweb)
 
 {
 
 $webLanguage = ""
 
 $installedLanguages = $SPweb.RegionalSettings.InstalledLanguages 
 
 foreach ($language in $installedLanguages) 
 
 { 
 
 if($language.LCID -eq $SPweb.Language)
 
 {
 
 $webLanguage = $language.DisplayName
 
 break
 
 }
 
 } 
 
 return $webLanguage
 
 } 
 
 ################ End of Method #################
 
 

The snippet is self-explanatory.

Happy Coding,

Sathish Nadarajan.

Categories