Export SharePoint Configuration Inventory to XML using PowerShell

Requirement: We had to compare specific SharePoint inventories between different environments, such as web applications, content databases, etc. So had to extract and export SharePoint configurations into an XML file.

PowerShell Script to Export Web Applications List into XML

Export SharePoint list data to XML using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)

#Create Root Node
[System.XML.XMLElement]$XMLRoot=$XMLDocument.CreateElement("WebApplications")

#Get all web applications and loop through
$WebApps = Get-SPWebApplication
ForEach($webApp in $WebApps)
{
    $WebAppElement = $XMLDocument.CreateElement("WebApplication")
    $WebAppElement.SetAttribute("Name", $WebApp.DisplayName)
    $WebAppElement.SetAttribute("URL", $WebApp.URL)
    #Append the data to Root node
    $XMLRoot.AppendChild($WebAppElement)
}
# Append Root Node to XML
$XMLDocument.AppendChild($XMLRoot)

#Save File
$XMLDocument.Save("c:\WebApps.xml")

Script output:

sharepoint export data to xml

Inventory Web Applications and Its Content Databases associated with XML

Let’s add database information to it:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)

#Create Root Node "WebApplications"
[System.XML.XMLElement]$WebAppRootElement=$XMLDocument.CreateElement("WebApplications")

#Get all web applications and loop through
$WebApps = Get-SPWebApplication
ForEach($webApp in $WebApps)
{
    $WebAppElement = $XMLDocument.CreateElement("WebApplication")
    $WebAppElement.SetAttribute("Name", $WebApp.DisplayName)
    $WebAppElement.SetAttribute("URL", $WebApp.URL)

    #Append the WebApplication data to "WebApplications" Node
    $WebAppRootElement.AppendChild($WebAppElement)

    #Database Element
    $DatabaseRootElement = $XMLDocument.CreateElement("Databases")

    $ContentDBs = $webApp.ContentDatabases
    ForEach($Database in $ContentDBs)
    {
        $DatabaseElement = $XMLDocument.CreateElement("Database") 
        $DatabaseElement.SetAttribute("Name", $Database.DisplayName)
        $DatabaseElement.SetAttribute("ID", $Database.ID)
           
        #Append the Database elements to "WebApplication" node
        $WebAppElement.AppendChild($DatabaseElement)
    }
}
# Append Root Node to XML
$XMLDocument.AppendChild($WebAppRootElement)

#Save File
$XMLDocument.Save("c:\WebApps-inventory.xml")

PowerShell to Inventory SharePoint Service Applications to XML

Let’s Inventory SharePoint 2016 Service Applications:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Create new XML File
[System.XML.XMLDocument]$XMLDocument=New-Object System.XML.XMLDocument

#Add XML Declaration
$Declaration = $XMLDocument.CreateXmlDeclaration("1.0","UTF-8",$null)
$XMLDocument.AppendChild($Declaration)

#Create Root Node "ServiceApplications"
[System.XML.XMLElement]$ServiceAppRootElement=$XMLDocument.CreateElement("ServiceApplications")

#Get all Service applications and loop through
$ServiceApps = Get-SPServiceApplication
Foreach($ServiceApp in $ServiceApps)
{
    $ServiceAppElement = $XMLDocument.CreateElement("ServiceApplication")
    $ServiceAppElement.SetAttribute("TypeName",$ServiceApp.TypeName)
    $ServiceAppElement.SetAttribute("DisplayName", $ServiceApp.DisplayName)
    $ServiceAppElement.SetAttribute("Status", $ServiceApp.Status)    

    #Get the Account and Proxy Group of the Service App
    $ServiceAccountElement = $XMLDocument.CreateElement("ServiceAccount")
    $ServiceAccountElement.Set_InnerText($ServiceApp.ApplicationPool.ProcessAccountName)
    #Append to "ServiceApplication" node
    $ServiceAppElement.AppendChild($ServiceAccountElement)

    $ServiceAppProxyElement = $XMLDocument.CreateElement("ProxyGroup")
    $ServiceAppProxyElement.Set_InnerText($ServiceApp.ServiceApplicationProxyGroup.FriendlyName)
    #Append to "ServiceApplication" node
    $ServiceAppElement.AppendChild($ServiceAppProxyElement)    

    #Append the Service Application data to "ServiceApplications" Node
    $ServiceAppRootElement.AppendChild($ServiceAppElement)
}
# Append Root Node to XML
$XMLDocument.AppendChild($ServiceAppRootElement)

#Save File
$XMLDocument.Save("c:\ServiceAppsInventory.xml")

and it produces the following output:

sharepoint export to xml

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *