SharePoint Online: Add-Remove Fields to List View using PowerShell

Requirement: SharePoint Online PowerShell to Update a List View.

How to Add or Remove Columns in SharePoint Online List View?

SharePoint Online list view enables you to arrange the information in the way that best suits your needs. You can customize a SharePoint Online view to add or remove fields from it. E.g., You may have a list with dozens of columns, and you may only need to view a subset of those columns in your day-to-day work. This blog post will learn how to add or remove fields from the view using PowerShell. This can be handy if you need to quickly focus on specific data in a large list.

To add or remove columns in the SharePoint Online list view, follow these steps:

  1. Navigate to your SharePoint Online List >> Click on “Edit current View” from the View drop-down. Add or Remove Fields to SharePoint Online List View using PowerShell
  2. On Modify View page, Select/Unselect the checkboxes next to columns based on your requirement sharepoint online powershell add column to view
  3. Click on Save to commit your changes. You can also show or hide columns from a view by selecting the Column settings >> Show/hide columns from any column dropdown. show hide column sharepoint view
  4. Simply select/unselect any field to make it visible or hide it from the list view.hide a column from sharepoint online list view

Now, let’s add a column to SharePoint Online view using PowerShell.

SharePoint Online: PowerShell to Add a Field to View

Here is the PowerShell to add a field to an existing List view in SharePoint Online:

#Load SharePoint Online 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/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="[email protected]"
$Password ="Password goes here"

Try {
    
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    #Get Web information and subsites
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the list
    $List = $Context.web.lists.GetByTitle($Listname)
    
    #Check if View has the field already
    $ViewFields = $List.DefaultView.ViewFields
    # Get a specific list view by Name
    #$view=$List.Views.getByTitle($ViewName)
    $Context.load($ViewFields)  
    $Context.ExecuteQuery()  

    if( ($ViewFields -Contains $FieldToAdd) -eq $false) {
        #Add the Field to View
        $List.DefaultView.ViewFields.Add($FieldToAdd)
        $List.DefaultView.Update()
        $Context.ExecuteQuery()

        Write-host "List View updated!" -ForegroundColor Green 
    }
    else { 
    write-host "Field exists in the view already!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

The above script adds the “ID” field to the default view of the given library

Add Column to List View using PnP PowerShell:

Let me show you how to add a field to your SharePoint Online list view using PnP PowerShell.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ColumnName = "Category" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Check if view doesn't have the column already
    If($ListView.ViewFields -notcontains $ColumnName)
    {
        #Add Column to View
        $ListView.ViewFields.Add($ColumnName)
        $ListView.Update()
        $Context.ExecuteQuery()
        Write-host -f Green "Column '$ColumnName' Added to View '$ViewName'!"
    }
    else
    {
        Write-host -f Yellow "Column '$ColumnName' Already Exists in View '$ViewName'!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Remove Field from the List View using PowerShell in SharePoint Online:

Similarly, to remove a field from the view, the PowerShell script goes like this:

#Load SharePoint Online 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/sites/Sales/"
$ListName="Documents"
$FieldToAdd="ID"
$UserName="[email protected]"
$Password ="Password goes here"

Try {    
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the list
    $List = $Context.web.lists.GetByTitle($Listname)
    
    #Check if the View has the field in it
    $ViewFields = $List.DefaultView.ViewFields
    $Context.load($ViewFields)  
    $Context.ExecuteQuery()  

    if( ($ViewFields -Contains $FieldToAdd) -eq $true) {
        #Add the Field to View
        $List.DefaultView.ViewFields.Remove($FieldToAdd)
        $List.DefaultView.Update()
        $Context.ExecuteQuery()

        Write-host "Field Has been removed from the List View!" -ForegroundColor Green 
    }
    else { 
    write-host "Field doesn't exist in the view!" -foregroundcolor Red 
    }

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Remove Column from SharePoint Online View using PnP PowerShell:

Let’s use PnP PowerShell to remove a column from view.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"
$ColumnName = "Category" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Get the Context
    $Context = Get-PnPContext

    #Get the List View from the list
    $ListView  =  Get-PnPView -List $ListName -Identity $ViewName -ErrorAction Stop

    #Check if view doesn't have the column already
    If($ListView.ViewFields -contains $ColumnName)
    {
        #Add Column to View
        $ListView.ViewFields.Remove($ColumnName)
        $ListView.Update()
        $Context.ExecuteQuery()
        Write-host -f Green "Column '$ColumnName' Removed from View '$ViewName'!"
    }
    else
    {
        Write-host -f Yellow "Column '$ColumnName' doesn't exist in View '$ViewName'!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

If you need SharePoint Online PowerShell to Update ListView, such as View Filter, refer: SharePoint Online: PowerShell to Update View

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

6 thoughts on “SharePoint Online: Add-Remove Fields to List View using PowerShell

  • How can you add multiple fields instead of one field #notadeveloper

    Reply
  • I used the last script (pnp) Works great but the If statement doesnt work for me. It will create duplicate columns if the script is re run.

    Any Ideas ?

    Reply
    • This happens, when you use “Display Name” instead of “Internal Name” for the $ColumnName variable!

      Reply
      • Thank you. Just noticed so I used the internal name for the loop. Working now. Thanks for the reply.

        Reply
  • Great article! Could you please guide me on how to apply versioning to all Document Libraries in the site collection?
    I’m a newbie to PS. Thanks, Jo.

    Reply

Leave a Reply

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