Today on Twitter I received a message from Nik Patel:
“How would I determine the site collection size using powershell? I am sure you have figured it out.. ;)”
As luck would have it, I had. The good news is that PowerShell exposes that information pretty easily. Even an admin like myself can figure it out. Let’s walk through how I put this all together. First I set a variable to a site collection so that I could take a look at the properties that were exposed.
$sc = get-spsite http://sharepoint
$sc | get-member
From here I saw the Usage property. That looks promising. executed $sc.usage did give me the storage used by a site collection, but it looked a bit more complicated:
PS C:\> $sc.Usage
Storage : 9621220
Bandwidth : 0
Visits : 0
Hits : 0
DiscussionStorage : 0
I only wanted the storage usage, so I needed a good way to pull all that out. Plus I needed it for all the site collections, not just one. That lead me to this line:
Get-SPSite | select url, @{label="Size in MB";Expression={$_.usage.storage}}
This gives me the information I wanted, but the presentation leaves a bit to be desired. This line worked a little better:
Get-SPSite | select url, @{label="Size in MB";Expression={$_.usage.storage/1MB}} | Sort-Object -Descending -Property "Size in MB" | Format-Table –AutoSize
This gave me the storage usage in megabytes instead of bytes, sorted the site collections by size, and formatted it all nicely. To go for the gold I decided to take it one step farther:
Get-SPSite | select url, @{label="Size in MB";Expression={$_.usage.storage/1MB}} | Sort-Object -Descending -Property "Size in MB" | ConvertTo-Html -title "Site Collections sort by size" | Set-Content sc.html
This takes the output from above and writes it to an HTML file for easier viewing.
You can view the entire transcript and the HTML output if you’d like.
Happy PowerShelling.
tk