March 24, 2011

A Very Helpful PowerShell Script

I don’t normally post code samples as I try to do just commentary here, but I created a PowerShell script today based extensively on a sample from Phil Childs’ PowerShell Scripts site.  My need was to change about 1000 SharePoint sites’ site collection quotas and the GUI doesn’t support bulk editing…this is what I ended up with:

function CreateQuotaTemplate ($Name, $MaxLevelMB, $WarnLevelMB)
$quotaTemplate = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate
$quotaTemplate.Name = $Name
$quotaTemplate.StorageMaximumLevel = ($MaxLevelMB*1024)*1024
$quotaTemplate.StorageWarningLevel = ($WarnLevelMB*1024)*1024
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

CreateQuotaTemplate –Name “Standard Site Collection Quota” –MaxLevelMB 900 –WarnLevelMB 850

function BulkReplaceQuotaTemplates ($WebApp, $NewTemplate)
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$newQuotaTemplate = $contentService.QuotaTemplates[$NewTemplate]
$webApplication = Get-SPWebApplication $WebApp
$webApplication | Get-SPSite -limit all | ForEach-Object {
Set-SPSite -Identity $_.Url -QuotaTemplate! $newQuotaTemplate}}

BulkReplaceQuotaTemplates -WebApp http://mywebapp -NewTemplate “Standard Site Collection Quota”

Very cool – thanks Phil – saved me hours of clicking…