SharePoint List – Delete Columns Through PowerShell

Today my colleague ran into an issue where she added columns to a SharePoint 2013 list, later realised they were not what she wanted and was unable to delete them.

I ended up doing up the following PowerShell script to delete the columns based on some examples I found online in both C# and PowerShell.  The key one I found was on SharePoint Diary; however, this did not work for me but was close to what I had already started.

The full script that worked for me is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Variables
$SiteURL="http://foundations.thrive"
$ListName="Activity Reporting - 2018"
$ColumnName="Target Audiences"
 
Write-Host $ListName - $ColumnName
#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.Sealed = $false
        $column.ReadOnlyField = $false
        $column.AllowDeletion = $true
        $column.Update()
 
        #Delete the column from list
        $list.Fields.Delete($column.InternalName)
        write-host "Column has been deleted!" -f Green
    }
    else
     {
        write-host "Specified column name not found!" -ForegroundColor Red
     }
}

The key difference from theirs is the column properties I had to set.  Setting the columns Hidden property:

1
$column.Hidden = $false

Didn’t work for me and I had to replace it with:

1
$column.Sealed = $false

This might depend on the type of column you are having issues with, but since I we were unable to delete these through the GUI this is what I ended up doing but wanted to document the other way as well encase down the road we run into a scenario that fits there original use case.

Leave a Reply

Your email address will not be published. Required fields are marked *