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:

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>

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 used fields for List view only. For fetching all list fields, you will need to do below modification.

    $site = new-object Microsoft.SharePoint.SPSite("http://&lt;Site URL&gt;") #Change site URL# 
    $web = $site.openweb()  
    $list = $web.Lists["&lt;List Name&gt;"] #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()

The following two tabs change content below.
Adnan Amin
Adnan is an active SharePoint MVP and having over 11 years of extensive experience with major expertise on SharePoint Server, office 365, .Net Platform and Microsoft BI, Adnan is currently working as SharePoint Architect at FMT Consultants. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past seven years and worked on different intranet/intranet solutions for private & govt. sector majorly in United States and Gulf region, which include OOB and customized solutions. He is a trainer, technology evangelist and also speaks in community forums.

One Comment

Leave a Reply

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