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:
#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:
$column.Hidden = $false
Didn’t work for me and I had to replace it with:
$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.
Hi there,
I am getting the following error:
You cannot call a method on a null-valued expression.
At line:10 char:1
+ $list = $web.Lists.TryGetList($ListName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Do you know how it can be solved?
Hi Albert,
You need to make sure that the $SiteUrl and $ListName for your environment are correct. If you check the $web variable after the intialization line (line #8), is it $null or does it contain a reference to a SP Web?