Tuesday, 18 August 2015

Get Site Collections per Content Database one-liner


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!