How to Remove a Column from SharePoint List using PowerShell?
Requirement: User created a calculated column with error in its formula! That caused the list view web part to crash! couldn't remove the column from SharePoint list using web UI.
How to delete a Column from SharePoint List?
From SharePoint Web UI, to remove a field from list or library, follow these steps:
PowerShell script to delete the column from SharePoint list
Here is the PowerShell to remove a column from list.
Bulk remove columns from SharePoint list using PowerShell:
To make it reusable, lets wrap the code inside a function and call, to bulk delete columns from SharePoint list.
How to delete a Column from SharePoint List?
From SharePoint Web UI, to remove a field from list or library, follow these steps:
- Browse to the List or Library in which you want to remove column. click on List or Library tab.
- Click on List Settings / Library Settings from the Ribbon.
- From the List Settings page, Under Columns section, click on the name of the column that you wish to delete.
- Scroll down to the bottom and then click on "Delete" button. Confirm the prompt.
PowerShell script to delete the column from SharePoint list
Here is the PowerShell to remove a column from list.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Variables $SiteURL="http://intranet.crescent.com/" $ListName="Change Request" $ColumnName="Activity Status" #Get Internal Name of the columns $web = Get-SPWeb $SiteURL #Get the list $list = $web.Lists.TryGetList($ListName) if($List -ne $null) { #Get the column $column = $list.Fields[$ColumnName] if($column -ne $null) { #Reset column properties to allow delete $column.Hidden = $false $column.ReadOnlyField = $false $column.AllowDeletion = $true $column.Update() #Delete the column from list $list.Fields.Delete($column) write-host "Column has been deleted!" -f Green } else { write-host "Specified column name not found!" -ForegroundColor Red } }
Bulk remove columns from SharePoint list using PowerShell:
To make it reusable, lets wrap the code inside a function and call, to bulk delete columns from SharePoint list.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue #Custom Function to delete a column with PowerShell Function Remove-Column($WebURL, $ListName, $ColumnName) { #Get Internal Name of the columns $web = Get-SPWeb $WebURL #Get the list $list = $web.Lists.TryGetList($ListName) if($List -ne $null) { #Get the column $column = $list.Fields[$ColumnName] if($column -ne $null) { #Reset column properties to allow delete $column.Hidden = $false $column.ReadOnlyField = $false $column.AllowDeletion = $true $column.Update() #Delete the column from list $list.Fields.Delete($column) write-host "Column '$ColumnName' has been deleted!" -f Green } else { write-host "Specified column name not found!" -ForegroundColor Red } } else { write-host "Specified List is not found!" -ForegroundColor Red } } #Variables $WebURL="http://intranet.crescent.com" $ListName="Change Request" #Array to hold column titles [email protected]("Change Type","Approvers","Proposed Date and Time") #Process each column $ColumnNames | foreach { #Call the function to remove column Remove-Column $WebURL $ListName $_ }
In some cases, instead of "$Column.Hidden=$False", You may have to set: "$column.Sealed = $false"
Hi folks,
ReplyDeleteYour code is awesome but unfortunately the Update doesn't work :(
Try: $column.Sealed = $false
Deleteyup, that works, thanks a lot
DeleteThanks, works very fine! Nice script.
ReplyDeleteto remove the column from all sites, how can I change the script?
ReplyDelete