Windows PowerShell Script to Output Site Collection Information

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
Comments (32)

  1. @Shepp – The script only reports Site Collections and doesn’t include information on sub-sites.

  2. Anonymous says:


    i am Getting the following Error :

    You cannot call a method on a null-valued expression.
    At C:ViralSiteInventorySPInventory.ps1:14 char:21
    + $Web=$Site.Rootweb; $Site.Url + "," + $Site.Owner.Name + "," +
    $Site.Owner.Email …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    Can you please let me know why is it so?

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

  4. 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.



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

  6. 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.

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

  8. Anonymous says:

    Thanks Brendan… 🙂
    Very useful script… I executed and it worked successfully.

  9. PowerShell User says:

    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?

  10. Praphull says:

    Thanks !!! its very much useful script ……

  11. Carter James says:

    I am using SharePoint 2013. Should this script work?

  12. Subhadarshini says:

    Thanks,Its very useful..:-)

  13. Lynn says:

    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?

  14. Lynn says:

    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!

  15. Lynn says:

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

  16. Vikas Sukhija says:

    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.

  17. Simon says:

    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.

  18. Simon says:

    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?

  19. Bobbe says:

    For Sharepoint 2013, As Farm Administrator, using powershell – how does one add say a third etc. site collection administrator over and above the primary and Secondary owner. We do not want to change or bump an existing primary and secondary owner.

    The "trick" of setting the IsSiteAdmin property to true on the spUser object plucked out of the Site.AllUsers collection worked in SP2007 and SP2010, but with SP2013, trying this results in an exception stating that you have to already be Site collection administrator
    to set this property.

    At first, we figured that Set-SPSiteAdministration was up to the task, but it seems to only support setting the OwnerAlias or the SecondaryOwnerAlias.

    Please advise – google has not come to the rescue with a solution yet ;):)

  20. Shepp says:


    Great script! I was wondering, does this catch the Sub-Sites which reside under any given Site Collection? Thank you!!!


  21. Akash Arora says:

    Hi Brendan, In our project we need reports on monthly basis i.e. report to be generated on last day of each month, so is there any way to find out how much storage quota has the Site collection consumed in a month.

  22. @Akash – You could compare against the figures from the previous month’s report?

  23. Syed says:

    I’m getting an error on this "$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl)" Do I need to eidt this?

  24. @Syed – As long as you have updated the $SiteURL variable at the top of the script you should be good to go.

  25. @Syed – As long as you have updated the $SiteURL variable at the top of the script, you should be good to go.

  26. prasad says:

    I am getting below issue while executing the script.. can anybody suggest..
    You cannot call a method on a null-valued expression.
    At C:\solution\sample.ps1:21 char:24
    + $Web = $Site.Rootweb; $Site.Url + $Sep + $Site.ContentDatabase.Name +
    $Sep + $w …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    1. Looks as though either the site or one of the properties isn’t available, may be worth amending the script to determine the site that is causing the issue?

  27. Steve White says:

    Have you used this for the on-line version?

    1. Hi Steve.
      For SPOnline; you might find that running Get-SPOSite -Detailed provides you with the information you need. You could also export this information to a CSV file, just like in Brendans’ script.
      Many thanks,

  28. Hello, Brendan!

    Since we expect our CVS file come with columns configured you need to change “,” to “;” in the strings.

  29. Hi Brenden – Great effort, this helps a lot to export site collection details. Can you please share an updated script with possible export of sub-sites as well? Many thanks in advance.

    KR, Prajith.

Skip to main content