Share via


SharePoint: Get SPField details for SharePoint Lists using PowerShell

I have seen multiple posts on different forums to get details of list columns (like field type) for all SharePoint Lists or any specific list. I also uploaded script to Technet document gallery. This PowerShell script will show the fields defined in list Views. But with a little modification in code you can get details of all fields associated with SharePoint List.

There are two functions in attached PowerShell script:

1 - GetSPFieldDetailsForAllLists

This function takes one parameter (Site URL), and will print all fields in SharePoint site associated with list views.

function GetSPFieldDetailsForAllLists($SiteCollectionURL) 
{ 
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) #Change site URL# 
    $web = $site.openweb()  
     
    foreach ($list in $web.Lists) #Get all list in web 
    { 
        foreach ($view in $list.Views) #Get all views in lists 
        { 
            $spView = $web.GetViewFromUrl($view.Url) #Grab views URL 
            Write-Host "List Name: " $list.Title  ##Print List title 
            Write-Host "------------------------------------------------------" 
            Write-Host "Field Name | Field Title " -ForegroundColor DarkGreen 
            Write-Host "------------------------------------------------------" 
            foreach ($spField in $spView.ViewFields) #Loop through all view URLs and get Fields (columns) 
            { 
                foreach ($field in $list.Fields) #Get all fields in lists 
                { 
                    if($spField -eq $field.Title) #if field in lists equals field in views 
                        { 
                            Write-Host $spField " | " $field.Type -ForegroundColor Green #Write out each field (column)                         
                        } 
                } 
            } 
            Write-Host "------------------------------------------------------" 
            Write-Host " " 
        } 
    } 
    $web.Dispose() 
    $site.Dispose() 
}

Function Call: Function can be called as GetSPFieldDetailsForAllLists http://<siteURL>

2 - GetSPFieldDetailsForList

This function takes two parameter (Site URL & List name). It's functionality is same as of GetSPFieldDetailsForAllLists but it will print field details of only single list which is passed as parameter.

function GetSPFieldDetailsForList($SiteCollectionURL, $listName) 
{ 
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) #Change site URL# 
    $web = $site.openweb()  
    $list = $web.Lists[$listName] #Get Field Details for specified list 
     
    foreach ($view in $list.Views) #Get all views in lists 
    { 
        $spView = $web.GetViewFromUrl($view.Url) #Grab views URL 
        Write-Host "List Name: " $list.Title  ##Print List title 
        Write-Host "------------------------------------------------------" 
        Write-Host "Field Name | Field Title " 
        Write-Host "------------------------------------------------------" 
        foreach ($spField in $spView.ViewFields) #Loop through all view URLs and get Fields (columns) 
        { 
            foreach ($field in $list.Fields) #Get all fields in lists 
            { 
                if($spField -eq $field.Title) #if field in lists equals field in views 
                { 
                    Write-Host $spField " | " $field.Type -ForegroundColor Green #Write out each field (column)                         
                } 
            } 
        } 
        Write-Host "------------------------------------------------------" 
        Write-Host " " 
    }     
    $web.Dispose() 
    $site.Dispose() 
}

Function Call: Function can be called as GetSPFieldDetailsForList http://<siteURL> <List Name>

Get Details of all Fields associated with SharePoint List:

With a little modification in above code, you can get details of all fields associated with List. In above code snippet I have fields for List view only. For fetching all list fields, you will need to do below modification.

$site = new-object Microsoft.SharePoint.SPSite("http://<Site URL>") #Change site URL# 
    $web = $site.openweb()  
    $list = $web.Lists["<List Name>"] #Get Field Details for specified list 
     
    Write-Host "List Name: " $list.Title  ##Print List title 
    Write-Host "------------------------------------------------------" 
    Write-Host "Field Name | Field Title " 
    Write-Host "------------------------------------------------------"         
     
    foreach ($field in $list.Fields) #Get all views in lists 
    { 
        Write-Host $field.Title " | " $field.Type -ForegroundColor Green #Write out each field (column)       
    }    
    Write-Host "------------------------------------------------------" 
    Write-Host " "  
 
    $web.Dispose() 
    $site.Dispose()