Tuesday, February 12, 2013

Create SharePoint Site Quota Templates using PowerShell

Original article

Here you can find PowerShell script that creates and changes Quota Template for Site Collection. You can find full original article here

Create SharePoint Site Quota Templates using PowerShell


function New-SPQuotaTemplate {
<#
    This advanced function creates a new Site Quota Template.
    This function uses .NET code to instantiate an instance of an
    SPQuotaTemplate class. Once the object is created, an instance of the
    SPWebService class is instantiated and the Quota Template is added to the
    Quota Templates Collection.
.Example
    C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 2GB -StorageWarningLevel 1GB -UserCodeMaximiumLevel 100 -UserCodeWarningLevel 75
    This example creates an SP Quota Template called Custom with a maximum size
    of 2GB and a warning size of 1GB. Sandboxed solutions are
    limited to 100, with a warning level of 75.
.Example
    C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 4GB -StorageWarningLevel 3GB
    This example creates an SP Quota Template called Custom with a maximum size
    of 4GB and a warning size of 3GB
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$Name,
[Parameter(Mandatory=$true)][Int64]$StorageMaximumLevel,
[Parameter(Mandatory=$true)][Int64]$StorageWarningLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeMaximumLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeWarningLevel
)
# Instantiate an instance of an SPQuotaTemplate class #
Write-Host "Instantiating an instance of an SPQuotaTemplate class"
$Quota = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate
# Set the Properties #
Write-Host "Setting properties on the Quota object"
$Quota.Name = $Name
$Quota.StorageMaximumLevel = $StorageMaximumLevel
$Quota.StorageWarningLevel = $StorageWarningLevel
$Quota.UserCodeMaximumLevel = $UserCodeMaximumLevel
$Quota.UserCodeWarningLevel = $UserCodeWarningLevel
# Get an Instance of the SPWebService Class #
Write-Host "Getting an instance of an SPWebService class"
$Service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
if ($Service.QuotaTemplates[$Name] -eq $null )
{
# Use the Add() method to add the quota template to the collection #
Write-Host "Adding the $($Name) Quota Template to the Quota Templates Collection"
$Service.QuotaTemplates.Add($Quota)
# Call the Update() method to commit the changes #
$Service.Update()
}
else
{
Write-Host "Quota Template $Name exists"
}
}

To change the quota template for a site collection by using Windows PowerShell

function Set-SPQuotaTemplate ($SiteCollectionName,$QuotaTemplateName)
{
    Set-SPSite -Identity $SiteCollectionName -QuotaTemplate $QuotaTemplateName 
Write-Host "Quota template $QuotaTemplateName was set to site collection $SiteCollectionName"
}