PowerShell Script to Create a SharePoint Quota Template

PowerShell script to create a SharePoint quota template, edit the highlighted are in yellow to set the maximum and minimum storage limit for the quota template.

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\CreateQuotaTemplatesPatch-$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. $name = ""  
  29. $storageMaximumLevel =""  
  30. $storageWarningLevel =""  
  31.   
  32. [String]$name = Read-Host "Enter the new quota Template Name: [e.g. Custom]"  
  33. [int64]$storageMaximumLevel = 10GB  
  34. [int64]$storageWarningLevel = 9GB  
  35.   
  36. try {  
  37.   
  38.    Write-host "Instantiating an instance of an SPQuotaTemplate class"  
  39.    $quota = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate  
  40.    Write-host "Setting properties on the Quota object"  
  41.    $quota.Name = $name  
  42.    $quota.StorageMaximumLevel = $storageMaximumLevel  
  43.    $quota.StorageWarningLevel = $storageWarningLevel  
  44.    Write-host "Getting an instance of an SPWebService class"  
  45.    $service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService  
  46.    Write-host "Adding the $($Name) Quota Template to the Quota Templates Collection"  
  47.    $service.QuotaTemplates.Add($quota)  
  48.    $service.Update()  
  49. }  
  50. catch [Exception] {  
  51.    Write-Error $Error[0]  
  52.    $err = $_.Exception  
  53.    while ( $err.InnerException ) {  
  54.       $err = $err.InnerException  
  55.       Write-Output $err.Message  
  56.    }  
  57. }