SharePoint Online: Delete Column from List using PowerShell

Requirement: SharePoint Online delete column from list

How to remove a column from SharePoint Online List?
We can delete Columns from lists or libraries when they are no longer needed. To delete a list column, follow these steps:
  • Go to List settings by going to List tab >> Under Settings group, click on List Settings button in the ribbon.
  • Under the List Settings page, in the Columns section, click on the column title you wish to delete.
  • Scroll down and Click on Delete button.
    sharepoint online list delete column
  • Confirm the deletion prompt by clicking OK.
This deletes SharePoint Online list column.
Please note, When you delete a column in SharePoint, all the data saved in the column is also deleted! We can't restore this column data from recycle bin!

SharePoint Online: Delete List Column using PowerShell
Here is the PowerShell to delete a column from List in SharePoint Online
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteURL="https://crescent.sharepoint.com"
$ListName= "Projects"
$ColumnName="Project Code" #Display Name of the column

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)

    #Get the Column to delete
    $Column = $List.Fields.GetByTitle($ColumnName)
    $Column.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "Column '$ColumnName' deleted Successfully!" -ForegroundColor Green
 }
Catch {
    write-host -f Red "Error Deleting Column from List!" $_.Exception.Message
}

PnP PowerShell to Remove a Column from SharePoint Online List:
Here is how to remove a column from list using PnP PowerShell in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName= "Team Projects"
$ColumnName= "ProjectStartDate" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #sharepoint online powershell delete list column
    Remove-PnPField -Identity $ColumnName -List $ListName -Force -ErrorAction Stop
    Write-host "Column '$ColumnName' Removed from the List Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
SharePoint Online: Delete Column from List using PowerShell SharePoint Online: Delete Column from List using PowerShell Reviewed by Salaudeen Rajack on December 18, 2016 Rating: 5

2 comments:

  1. This is exactly what I have been searching for!

    ReplyDelete
  2. Great Help! The only thing I found out for "sealed" columns is to add these additional lines to the Column Section and the Update Command:
        #Get the Column to delete
        $Column = $List.Fields.GetByTitle($ColumnName)
    $Column.Hidden = $false
    $Column.ReadOnlyField = $false
    $Column.Sealed = $false
    $Column.Update()
        $Column.DeleteObject()
        $Ctx.ExecuteQuery()

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.