Tuesday, September 17, 2019

Sharepoint - Powershell to display all Site Collection Quotas

Powershell Script to display all Sharepoint Site Collections quotas (disk spaced used) from selected WebApps.

Minor editing required: 
This script reports from a fixed list of Webapps.  The script shows results from a webapp called "http://moss" and others.  See the bottom of the PS1 script, under "Main".  Edit web-apps to meet company requirements.

Run from an Administrative Powershell session on any Sharepoint App Server.


Example Output:
















Outputs:
- Console Report (illustrated) - Sorted by In-Use
- CSV export with full details (not illustrated)


PS Script:

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }

# Generate a console and CSV quota report for all Site Collections in common SP2010 web apps.
# No need to edit script

# Designed for SP2010
#
# This script depends on .\append.ps1, in the same directory
# To run: Open Sharepoint Admin Powershell Console; run as Administrator
# PSP10app01 E:\SharePoint\PowerShell\QuotaReports: .\quotareport.ps1 
# Output: CSV file (see top of console report)
# Output: Console report showing %Occupied)
# Author: Terry Henry, Aaron G., Tim Wolf


#Include local current-directory script * Required!
. .\append.ps1

$script:webapplication = ""  #See main module, below
$script:outputfile = "QuotaReport.csv"
$script:iSiteCollectionCount = -1

$script:aSiteCollections = @()


# Onscreen Reporting arrays:

# Delete previous output file
  try
  {
   If (Test-Path $script:outputfile)
   {
     Remove-Item $script:outputfile
   }
  }
  catch [System.Exception]
  {
   Write-Host Unable to delete previous output file $_.Exception.Message 
  }


# ----------------------------------------------------------------------------
# Functions
# ----------------------------------------------------------------------------

function Report-SiteStats
{
  try
  {
   if ($webapplication.toLower() -eq "all")
   {
      $databases = Get-SPContentDatabase
   }
   else
   {
      $databases = Get-SPContentDatabase -WebApplication $script:webapplication
   }

   foreach ($database in $databases)
   {
    $script:iSiteCollectionCount++

    #Diagnostics
    #Write-host $script:iSiteCollectionCount $dbresults.url

      $dbresults = Get-SPSite -ContentDatabase $database.Name -Limit All | Select ID, URL, `
         #@{Name="Site Owner"; Expression={$_.Owner.Email}}, `
         @{Name="Site Owner"; Expression={$_.Owner.DisplayName}}, `
         @{Name="Secondary Owner"; Expression={$_.SecondaryContact.DisplayName}}, `
         @{Name="Quota"; Expression={"{0:N0}" -f ($_.Quota.StorageMaximumLevel/1GB)}}, `
         @{Name="StorageUsed"; Expression={"{0:N0}" -f ($_.Usage.Storage/1GB)}}, `
         @{Name="PercentUsed"; Expression={"{0:N0}" -f (  ($_.Usage.Storage/1GB) / ($_.Quota.StorageMaximumLevel/1GB )*100)}}, `
         #@{Name="Last Modified"; Expression={$_.LastModifiedDate = $web.LastItemModifiedDate.ToString('d')}}, `
         @{Name="Last Modified"; Expression={$_.LastContentModifiedDate}}, `
         @{Name="Last Modified Year"; Expression={$_.LastContentModifiedDate.Year}}, `
         @{Name="Last Modified Month"; Expression={$_.LastContentModifiedDate.Month}}, `
         @{Name="Last Modified Day"; Expression={$_.LastContentModifiedDate.Day}}, `
         @{Name="ContentDB"; Expression={$_.ContentDatabase.Name}} 


    if($dbresults -eq $null)
    {
      # Ignore empty Site Collections
    }
    else
    {
      $dbresults | export-csv -path $script:outputfile -NoTypeInformation -append

      #Store in an array for later reporting...
      $script:aSiteCollections += $dbresults | Select URL,'Quota','StorageUsed',@{Name="PercentUsed"; Expression={[int]$_.'PercentUsed'}}           

      #Example code when calling by @.FieldName (esp if fields have embedded spaces)
      #$script:aSiteCollections[$script:iSiteCollectionCount] = $dbresults.'Quota'
      #$script:aSiteCollections[$script:iSiteCollectionCount] = $dbresults.'PercentageUsed'
    }
   }

  }
  catch [System.Exception]
  {
   Write-Host $_.Exception.Message 
  }
}

# ----------------------------------------------------------------------------
# Main routine
# ----------------------------------------------------------------------------

Clear-Host
write-Host Generating Site Collection Quota Report...
Write-host 

$webapplication = "http://moss"
write-host $webapplication
Report-SiteStats

$webapplication = "http://CML"
write-host $webapplication
Report-SiteStats

$webapplication = "http://JIVA"
write-host $webapplication
Report-SiteStats

$webapplication = "http://TEAMS"
write-host $webapplication
Report-SiteStats

Write-Host
Write-Host Site Collections by Percentage Quota Used:
$aSiteCollections | sort-object "PercentUsed" -Descending| format-table -AutoSize
Write-Host Empty Site Collections not listed

Write-host csv written to current directory
write-host Done



My thanks to Aaron G. for his assistance.

-end