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 – http://technet.microsoft.com/en-us/library/ff678226.aspx. 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.

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
#Configure the location for the output file
$Output=”C:\Output.csv”;
“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
$Siteurl=”http://intranet.contoso.com“;
$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp=$Rootweb.Webapplication;
#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 (28)

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

  2. Anonymous says:

    Hi,

    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.

    Thanks

    Brendan

  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. DJ Grijalva says:

    Thanks!

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

  9. Anonymous says:

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

  10. 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?

  11. Praphull says:

    Thanks !!! its very much useful script ……

  12. Carter James says:

    I am using SharePoint 2013. Should this script work?

  13. Subhadarshini says:

    Thanks,Its very useful..:-)

  14. 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?

  15. 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!

  16. Lynn says:

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

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

    http://msexchange.me/2013/05/29/publish-site-collection-administrator-report/

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

  19. 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:
    http://act2010documents.customer.com/country/public,,,7/27/2014,0,12,0

    Any idea?

  20. 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 ;):)

  21. Shepp says:

    Brendan,

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

    Shepp

  22. 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.
    Thanks
    Akash.

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

  24. Syed says:

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

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

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

  27. 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?