Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Powershell script to iterate through all the sites in SharePoint Farm
WhatsApp
Karthik Muthu Karuppan
Feb 04
2015
6.4
k
0
0
SiteDetailsInXMLForm
PowerShell script to iterate through all the sites in the SharePoint farm and write the output to XML file,
Script:
$LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
$LogFile =
".\SiteDetailsInXMLFormatPatch-$LogTime.rtf"
# Add SharePoint PowerShell Snapin
if
( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $
null
) {
Add-PSSnapin Microsoft.SharePoint.Powershell
}
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase
#Deleting any .rtf files in the scriptbase location
$FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
if
($FindRTFFile)
{
foreach($file
in
$FindRTFFile)
{
remove-item $file
}
}
start-transcript $logfile
$SiteCollection = get-spsite -limit all
# Set the File Name
$filePath = $scriptbase +
"\" + "
SiteDetails.xml"
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
# Write Root Element
$xmlWriter.WriteStartElement(
"SiteCollectionDetails"
) #Opening site collection details
foreach($site
in
$siteCollection)
{
write-host
"Collecting information for the site collection "
$site.url -fore magenta
$xmlWriter.WriteStartElement(
"SiteCollection"
) #Opening SiteCollection URL
$xmlWriter.WriteAttributeString(
"URL"
, $Site.url)
$xmlWriter.WriteAttributeString(
"SubSiteCount"
, $Site.allwebs.count)
foreach($web
in
$site.allwebs)
{
write-host
"collecting information for the web "
$web.url -fore blue
$xmlWriter.WriteStartElement(
"Webs"
) #Opening Webs
$xmlWriter.WriteAttributeString(
"URL"
, $web.url)
$xmlWriter.WriteAttributeString(
"ListCount"
, $web.lists.count)
foreach($list
in
$web.lists)
{
write-host
"Collecting information for the list "
$list.title -fore yellow
$xmlWriter.WriteStartElement(
"List"
) #Opening List
$xmlWriter.WriteAttributeString(
"Title"
, $List.Title)
$xmlWriter.WriteAttributeString(
"ItemCount"
, $list.items.count)
foreach($item
in
$list.items)
{
write-host
"Processing item "
$item.id -fore cyan
$xmlWriter.WriteStartElement(
"Item"
) #Opening Item
$xmlWriter.WriteAttributeString(
"ID"
, $Item.ID)
$xmlWriter.WriteAttributeString(
"VersionCount"
, $Item.versions.count)
$xmlWriter.WriteEndElement() #Closing Item
}
$xmlWriter.WriteEndElement() #Closing List
}
$xmlWriter.WriteEndElement() #closing Webs
}
$xmlWriter.WriteEndElement() #Closing SiteCollection URL
}
$xmlWriter.WriteEndElement() #closing site collection details
# End the XML Document
$xmlWriter.WriteEndDocument()
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush
$xmlWriter.Close()
write-host
"SCRIPT COMPLETED"
-fore green
stop-transcript
SharePoint
XML
PowerShell
Up Next
Powershell script to iterate through all the sites in SharePoint Farm