PowerShell Script to Set Quota Template to a SharePoint Site Collection

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\SetQuotaTemplatePatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.   foreach($file in $FindRTFFile)  
  20.   {  
  21.    remove-item $file  
  22.   }  
  23. }  
  24.   
  25.   
  26. start-transcript $logfile  
  27.   
  28. [string]$url = Read-Host "Enter the site collection url: [e.g. http://server_name]"  
  29. [string]$template = Read-Host "Enter the quota template name that has to be used: [e.g. MyTemplate]"  
  30.   
  31. try {  
  32.       
  33.    Write-host "Configuring site collection to use specified quota template ..."  
  34.    $quotaTemplate = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.QuotaTemplates |   
  35.    Where-Object {$_.Name -eq $template}  
  36.    Set-SPSite -Identity $url -QuotaTemplate $quotaTemplate  
  37. }  
  38. catch [Exception] {  
  39.    Write-Error $Error[0]  
  40.    $err = $_.Exception  
  41.    while ( $err.InnerException ) {  
  42.       $err = $err.InnerException  
  43.       Write-Output $err.Message  
  44.    }  
  45. }