共用方式為


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 - https://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="https://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

  • Anonymous
    January 01, 2003
    @Shepp - The script only reports Site Collections and doesn't include information on sub-sites.

  • Anonymous
    January 01, 2003
    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?

  • Anonymous
    January 01, 2003
    $site.rootweb.title should give you this information.

  • Anonymous
    January 01, 2003
    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

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

  • Anonymous
    January 01, 2003
    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.

  • Anonymous
    January 01, 2003
    Thanks!

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

  • Anonymous
    January 01, 2003
    Thanks Brendan... :)
    Very useful script... I executed and it worked successfully.

  • Anonymous
    August 12, 2013
    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?

  • Anonymous
    October 17, 2013
    Thanks !!! its very much useful script ......

  • Anonymous
    November 22, 2013
    I am using SharePoint 2013. Should this script work?

  • Anonymous
    February 07, 2014
    Thanks,Its very useful..:-)

  • Anonymous
    March 03, 2014
    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?

  • Anonymous
    March 06, 2014
    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!

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

  • Anonymous
    March 15, 2014
    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/

  • Anonymous
    July 09, 2014
    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.

  • Anonymous
    July 27, 2014
    The comment has been removed

  • Anonymous
    August 01, 2014
    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 ;):)

  • Anonymous
    October 23, 2014
    Brendan,

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

    Shepp

  • Anonymous
    December 16, 2014
    The comment has been removed

  • Anonymous
    December 17, 2014
    @Akash - You could compare against the figures from the previous month's report?

  • Anonymous
    September 28, 2015
    I'm getting an error on this "$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl)" Do I need to eidt this?

  • Anonymous
    September 30, 2015
    @Syed - As long as you have updated the $SiteURL variable at the top of the script you should be good to go.

  • Anonymous
    September 30, 2015
    @Syed - As long as you have updated the $SiteURL variable at the top of the script, you should be good to go.

  • Anonymous
    July 10, 2016
    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

    • Anonymous
      July 19, 2016
      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?
  • Anonymous
    December 01, 2016
    The comment has been removed

    • Anonymous
      December 01, 2016
      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,Steve
  • Anonymous
    December 02, 2016
    Hello, Brendan!Since we expect our CVS file come with columns configured you need to change "," to ";" in the strings.

  • Anonymous
    July 09, 2018
    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.