Tuesday, November 26, 2024

Power shell Script to get all lists and library columns and its type to export in to CSV from a Sharepoint 2013/2016 site.

 This script includes additional columns for the field type and whether the field is hidden. It ensures that all fields, including hidden and system fields, are collected and exported to the CSV file.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


# Configuration Parameters

$SiteURL = "https://intranet.crescent.com/sites/test/"

$CSVPath = "D:\AllFields.csv"


# Get the Web

$Web = Get-SPWeb $SiteURL


# Create a DataTable to store the fields information

$DataTable = New-Object System.Data.DataTable

$DataTable.Columns.Add("ListTitle") | Out-Null

$DataTable.Columns.Add("FieldTitle") | Out-Null

$DataTable.Columns.Add("InternalName") | Out-Null

$DataTable.Columns.Add("FieldType") | Out-Null

$DataTable.Columns.Add("IsHidden") | Out-Null


# Iterate through each list and library

foreach ($List in $Web.Lists) {

    foreach ($Field in $List.Fields) {

        $Row = $DataTable.NewRow()

        $Row["ListTitle"] = $List.Title

        $Row["FieldTitle"] = $Field.Title

        $Row["InternalName"] = $Field.InternalName

        $Row["FieldType"] = $Field.TypeDisplayName

        $Row["IsHidden"] = $Field.Hidden

        $DataTable.Rows.Add($Row)

    }

}


# Export the DataTable to a CSV file

$DataTable | Export-Csv -Path $CSVPath -NoTypeInformation


# Dispose of the web object

$Web.Dispose()


Write-Host "All fields exported successfully to $CSVPath"


No comments:

Post a Comment