Script to get database names of associated site collection and Sub sites

sns 9,236 Reputation points
2023-03-02T19:25:49.7466667+00:00

I have list of sites collections and sub sites in csv file and I want output of csv file with database name associated with site collections and sub sites

is there any such script? If yes could you please share

thank u.

SharePoint Server
SharePoint Server
A family of Microsoft on-premises document management and storage systems.
2,335 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 35,066 Reputation points Microsoft Vendor
    2023-03-03T05:39:18.9666667+00:00

    Hi @sns,

    You can refer to following powershell script to get all sitecollections and subsites associated with database

    If ((Get-PSSnapin | where {$_.Name -match "SharePoint.PowerShell"}) -eq $null)    
    {    
        Add-PSSnapin Microsoft.SharePoint.PowerShell    
    }    
    Clear-Host    
        
           
    $webapps = Get-SPWebApplication    
    $Output = "C:\data.csv" #Output file    
         
        
    $SiteDataCollection = @()  # Array to store data    
    foreach ($webapp in $webapps){    
        
    $webappurl = $webapp.URL;    
    $webappname=$webapp.DisplayName    
    $sites=get-spsite -limit all -WebApplication $webapp.URL    
        
    foreach ($site in $sites)    
    {    
    $ContentDB = Get-SPContentDatabase -site $site.url    
        
                $SiteData = New-Object PSObject     
                $SiteData | Add-Member -type NoteProperty -name "Web Application URL" -value $webappurl    
                $SiteData | Add-Member -type NoteProperty -name "Web Application Name" -value $webappname  
                $SiteData | Add-Member -type NoteProperty -name "Site Count" -value $($ContentDB.CurrentSiteCount)     
                $SiteData | Add-Member -type NoteProperty -name "Site URL" -value $site.url       
                $SiteData | Add-Member -type NoteProperty -name "Database Name" -value $ContentDB.Name     
                $SiteData | Add-Member -type NoteProperty -name "Database Size" -value $($ContentDB.DiskSizeRequired /1024/1024)    
                #$SiteData | Add-Member -type NoteProperty -name "Date Created" -value $site.RootWeb.Created      
                #$SiteData | Add-Member -type NoteProperty -name "OWner" -value $site.Owner          
                    
                $SiteDataCollection += $SiteData    
      
    }    
    }    
        
    $SiteDataCollection | Export-Csv -Path $Output -Force    
        
    Write-Host "Successfully Completed" -ForegroundColor DarkRed  
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.