Get Site Collections per Content Database oneliner

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 Management Shell (As administrator), and let PowerShell do your work for you!

Get Site Collections per Content Database oneliner

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:

Get Site Collections per Content Database oneliner 2

That’s all there is to it!

10 thoughts on “Get Site Collections per Content Database oneliner

Leave a Reply

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