Get Site Collections per Content Database one-liner


This will be a very short post, as it contains only 1 line of PowerShell code, and is not very hard to understand. It shows you all content databases, and for each of the content databases, it will show you the site collections within this content database.

Get-SPContentDatabase | %{Write-Output "- $($_.Name)”; foreach($site in $_.sites){write-Output $site.url}}

That wasn’t so bad right? Just copy and paste this code into your SharePoint 2010 Management Shell (As administrator), and let PowerShell do your work for you!

image

Now let’s say you want to save this output to a .txt file, you can add “> C:\sitecollections.txt”.

The full command would be:

Get-SPContentDatabase | %{Write-Output "- $($_.Name)”; foreach($site in $_.sites){write-Output $site.url}} > C:\sitecollections.txt

This would create a .txt file with the same information:

image

That’s all there is to it!

Advertisements

6 thoughts on “Get Site Collections per Content Database one-liner

  1. Pingback: How to get all the Site Collections in a Content Database (PowerShell) | Led to Slaughter

  2. Pingback: List The Site Collections In A Content Database | SharePoint Geek

  3. Thanks for this very helpful pointer. It was *exactly* what I needed. Important to note: It might be obvious to some, but the only Content Databases that will display on the resulting output will be those that are online. If you’ve taken and DBs offline in Central Admin or via Powershell, they won’t show here. 🙂 Thanks again!

    • Hi Doug,

      All sites within a site collection are stored in the same database. So if your “http://sharepoint.domain.com/sites/hr” is in the “WSS_Content” database, the site “http://sharepoint.domain.com/sites/hr/subsite” will also be in the “WSS_Content” database.

      Hope this answers your question.

      Regards, Nico

  4. Pingback: Content Database FAQ – SharePoint 2010 – .NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s