Thursday, September 26, 2013

How To Delete a Site Column from all Document Libraries using Powershell

Use below Powershell script to Delete a Site Column

$web = Get-SPWeb http:/mywebapp/
$list = $web.Lists["Shared Documents"]
$field = $list.Fields["Column to be deleted"]
$field.AllowDeletion = “true”
$field.Sealed = “false”
$field.Delete()
$list.Update()
$web.Dispose()

or use below script to delete the site column from all Document Libraries


Get-SPWeb -site   http://spsite -Limit All | foreach {
$doclibtype = [Microsoft.SharePoint.SPBaseType]::DocumentLibrary
$doclibs = $_.GetListsOfType($doclibtype)
$doclibs.Count
for ($i=0; $i -lt $doclibs.Count; $i++)
{
$doclib = $doclibs[$i]
$doclib.Title
$fieldtype = [Microsoft.SharePoint.SPFieldType]::Choice
$doclib.Fields.Delete(“Column to be deleted”)
$doclib.Update()
#$view = $doclib.Defaultview
#$view.viewFields.Add("Column name")
#$view.Update()
Write-Host "Field Deleted"
}
}
$site.Dispose()

No comments:

Post a Comment