Monday, April 4, 2011

SharePoint 2010: PowerShell to delete a web part from the gallery

param($Identity = "", $Url = "")

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")


$SPSite = New-Object Microsoft.SharePoint.SPSite($Url)

$SPWeb = $SPSite.OpenWeb()
$WebPartGallery = $SPWeb.Lists["Web Part Gallery"]
$gall = $WebPartGallery.Items select Name
$count = $gall.Count
for ($i =0; $i -lt $count;$i++)
{
if ($gall[$i] -match $Identity)
{
$WebPartGallery.Items.Delete($i)
$SPWeb.Update()
Write-Host "$Identity deleted"
$SPWeb.Dispose()
}
}

No comments: