Real world experiences of SharePoint CSS engineers from Microsoft UK

Windows PowerShell Script to Output Site Collection Information

Windows PowerShell Script to Output Site Collection Information

  • Comments 26
  • Likes

Windows PowerShell is a fantastic tool; SharePoint 2010 has literally hundreds of different PowerShell Cmdlets that are available out of the box, if you don’t believe me check this out - What about MOSS 2007? Whilst there aren’t any native Cmdlets for MOSS 2007, PowerShell can be used to access the SharePoint object model directly instead and in most cases achieve the same objectives, this isn’t as daunting as it sounds; I’m not a developer but even I have been able to find my way around the object model and put together some useful scripts (at least to me anyway!)

I’ve recently been helping one of my customers write some PowerShell scripts to improve their reporting capabilities and reduce the burden of day to day SharePoint administration on the support team. One of the scripts that I’ve written analyses every site collection within a Web application. The purpose of the script was to identify sites that were no longer required (as they hadn’t been updated for a long time) or that had a large quota assigned but were only using a small proportion of this, so that a smaller quota could be assigned. The script outputs the following information for each site collection into a csv file:
  • URL
  • Owner login
  • Owner e-mail address
  • Last time that the root web was modified
  • The size of the quota assigned
  • The total storage used
  • The percentage of the quota being used

I've included the script below, all you need to do is to copy this into Notepad (or your text editor of choice), edit the two highlighted variables to match your requirements - $Output specifies the location to output the results to in csv format, $SiteURL specifies the URL of the root site collection, this will then be used to discover other site collections within the Web application. Once you have done this save the file with a .PS1 extension, for example ScriptName.PS1. The script can be run on any server in the SharePoint farm from within a Windows PowerShell window using .\ScriptName.PS1.

#Configure the location for the output file
"Site URL"+","+"Owner Login"+","+"Owner Email"+","+"Root Site Last Modified"+","+"Quota Limit (MB)"+","+"Total Storage Used (MB)"+","+"Site Quota Percentage Used" | Out-File -Encoding Default -FilePath $Output;
#Specify the root site collection within the Web app
$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
#Loops through each site collection within the Web app, if the owner has an e-mail address this is written to the output file
Foreach ($Site in $Webapp.Sites)
{if ($Site.Quota.Storagemaximumlevel -gt 0) {[int]$MaxStorage=$Site.Quota.StorageMaximumLevel /1MB} else {$MaxStorage="0"};
if ($Site.Usage.Storage -gt 0) {[int]$StorageUsed=$Site.Usage.Storage /1MB};
if ($Storageused-gt 0 -and $Maxstorage-gt 0){[int]$SiteQuotaUsed=$Storageused/$Maxstorage* 100} else {$SiteQuotaUsed="0"};
$Web=$Site.Rootweb; $Site.Url + "," + $Site.Owner.Name + "," + $Site.Owner.Email + "," +$Web.LastItemModifiedDate.ToShortDateString() + "," +$MaxStorage+","+$StorageUsed + "," + $SiteQuotaUsed | Out-File -Encoding Default -Append -FilePath $Output;$Site.Dispose()};

Below is an example of the script output.
 Brendan Griffin
  • This script looks very useful - thanks for writing it.  

    Unfortunately it seems to be a bit corrupted by the HTML formatting.

    Is it possible for you to add it as a downloadable text file to this post?

  • Hi, apologies for that. This was a copy/paste from my previous Blog. I have updated this and it should now work if you copy/paste direct from the page.



  • Thanks !!! its very much useful script ......

  • I am using SharePoint 2013. Should this script work?

  • I haven't tested on 2013 but it should work, it's actually probably easier to use the native cmdlets in SharePoint 2013 instead of this script to extract the information.

  • Thanks,Its very useful..:-)

  • This is a great script, but I'd like to output the Site Collection title too. $Site.Title provides an empty field. Is this possible please?

  • $site.rootweb.title should give you this information.

  • Thanks Brenda....I think the $site.rootweb.title will provide the title for the root site collection of the web app, but will be blank for all the site collections that sit under the root site collection.

    Any other pointers gratefully received!

  • That's correct, for sites that reside within the Site Collection you can use $Web.title.

  • I was a bit hasty, site.rootweb.title does work. Thanks Brenda!

  • In our environment we have used powershell & published the same kind of report on one of the site, which is accessible by L1 & L2 teams..The report is extracted in HTML & than published on Sharepoint Site.

  • Hi Brendan.

    I tried this and it works fine, except all text are in the same Excel cell and not separated like in your picture.
    Do you know if this can be fixed?

    Thank you.

  • Strange, it's possible an encoding issue. Can you remove -Encoding Default from the script and retry?

  • Sorry, no difference.
    The "title" is like this: Site URL,Owner Login,Owner Email,Root Site Last Modified,Quota Limit (MB),Total Storage Used (MB),Site Quota Percentage Used
    And info in cell:,,,7/27/2014,0,12,0

    Any idea?

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment