SharePoint Online: Get All List Fields using PowerShell

Requirement: SharePoint Online PowerShell to Get All List Fields

How to Get List Fields in SharePoint Online?

Do you ever need to get list columns from a SharePoint Online List? Maybe you wanted to know the internal name of a SharePoint Online column or some other properties? This blog post will show you how to use PowerShell and a web browser interface to get the internal name of a SharePoint Online list column.

You can get list columns in SharePoint Online by navigating to the list settings under the “Columns” section, and you’ll find all fields of the particular list.

sharepoint Online get all list fields

SharePoint Online: PowerShell to Get List Fields

Here is the SharePoint Online PowerShell to get list columns: Let’s use PowerShell to get list field properties and export to a CSV file, such as:

  • List field name
  • Field Type
  • Field ID
  • Field Internal Name
  • Field Schema XML
  • and Field description
#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"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"

$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 and list fields
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)

#sharepoint online powershell get list columns
$Ctx.Load($List.Fields)
$Ctx.ExecuteQuery()
        
#Iterate through each field in the list
Foreach ($Field in $List.Fields)
{   
    #Skip System Fields
    if(($Field.ReadOnlyField -eq $False) -and ($Field.Hidden -eq $False)) 
    {
       #get internal name of sharepoint online list column powershell 
       Write-Host $Field.Title : $Field.InternalName
    }
}

SharePoint Online: Get All Fields of a List and Export to CSV

Let’s get all list fields and export them to a CSV file.

#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"

#function to Get all fields from a SharePoint Online list or library
Function Get-SPOListFields()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )

    Try {
        $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)
        $Ctx.Load($List)
        $Ctx.Load($List.Fields)
        $Ctx.ExecuteQuery()
        
        #Array to hold result
        $FieldData = @()
        #Iterate through each field in the list
        Foreach ($Field in $List.Fields)
        {   
            Write-Host $Field.Title `t $Field.Description `t $Field.InternalName `t $Field.Id `t $Field.TypeDisplayName

            #Send Data to object array
            $FieldData += New-Object PSObject -Property @{
                    'Field Title' = $Field.Title
                    'Field Description' = $Field.Description
                    'Field ID' = $Field.Id 
                    'Internal Name' = $Field.InternalName
                    'Type' = $Field.TypeDisplayName
                    'Schema' = $Field.SchemaXML
                    }
        }
        Return $FieldData
    }
    Catch {
        write-host -f Red "Error Getting Fields from List!" $_.Exception.Message
    } 
} 

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CSVLocation ="C:\Temp\ListFields.csv"

#Call the function to get all list fields
Get-SPOListFields -SiteURL $SiteURL -ListName $ListName | Export-Csv $CSVLocation -NoTypeInformation

PnP PowerShell to Get All Fields and Internal Names from a List

Do you need to know the column’s internal name in a SharePoint Online list? If so, Here is the PowerShell to get all fields along with field name, Internal name, and GUID from a SharePoint Online list.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName ="Projects"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Get All Columns from List
    Get-PnPField -List $ListName
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
sharepoint online powershell get list fields

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “SharePoint Online: Get All List Fields using PowerShell

  • Hi,
    I need a script to export all the list and library name, Column Internal Name, Column Display Name, Column Type from a site collection to excel without specifying a list/library name, could you please help?

    Thanks!

    Reply

Leave a Reply