TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Powershell script to iterate through all the sites in SharePoint Farm
Karthik Muthu Karuppan
Feb 04
2015
Code
6.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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