can yougive me exact powershell script to export sharepointlist

Demo User 0 Reputation points
2024-04-05T05:43:45.7533333+00:00

Add - PSSnapin Microsoft.SharePoint.PowerShell

function CreateCustomList($siteCollectionUrl, $listName) {

$spWeb = Get - SPWeb - Identity $siteCollectionUrl  

$spTemplate = $spWeb.ListTemplates["Custom List"]  

$spListCollection = $spWeb.Lists  

$spListCollection.Add($listName, $listName, $spTemplate)  

$path = $spWeb.url.trim()  

$spList = $spWeb.GetList("$path/Lists/$listName")  



#adding the field type(Number) to the list  

$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number  

$spList.Fields.Add("Area", $spFieldType, $false)  



#adding the field type(Number) to the list  

$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number  

$spList.Fields.Add("Population", $spFieldType, $false)  

$spList.Update()  

}

$siteCollectionUrl = "https://nextdigitlabs.sharepoint.com/sites/TimeTracking"

$listName = "Timesheet"

CreateCustomList $siteCollectionUrl $listName

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 51,836 Reputation points Microsoft External Staff
    2024-04-05T10:34:42.3533333+00:00

    Whether you use SharePoint online or server?

    For SharePoint online, run following PowerShell in the SharePoint Online Management Shell:

    #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"
       
    ##Variables for Processing
    $SiteUrl = "https://tenant.sharepoint.com/sites/emilytest"
    $ListName= "list"
    $ExportFile ="c:\ListItems.csv"
     
    #Get Credentials to connect
    $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)
      
    #Get All List Items
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $ListItems = $List.GetItems($Query)
    $FieldColl = $List.Fields
    $Ctx.Load($ListItems)
    $Ctx.Load($FieldColl)
    $Ctx.ExecuteQuery()
      
    #Array to Hold List Items
    $ListItemCollection = @()
      
    #Fetch each list item value to export to excel
    Foreach($Item in $ListItems)
    {
        $ExportItem = New-Object PSObject
        Foreach($Field in $FieldColl)
        {
                if($NULL -ne $Item[$Field.InternalName])
                {
                    #Expand the value of Person or Lookup fields
                    $FieldType = $Item[$Field.InternalName].GetType().name
                    if (($FieldType -eq "FieldLookupValue") -or ($FieldType -eq "FieldUserValue"))
                    {
                        $FieldValue = $Item[$Field.InternalName].LookupValue
                    }
                    else
                    {
                        $FieldValue = $Item[$Field.InternalName]  
                    }
                }
                $ExportItem | Add-Member -MemberType NoteProperty -name $Field.InternalName -value $FieldValue
        }
        #Add the object with above properties to the Array
        $ListItemCollection += $ExportItem
    }
    #Export the result Array to CSV file
    $ListItemCollection | Export-CSV $ExportFile -NoTypeInformation
      
    Write-host "List data Exported to CSV file successfully!"
    

    For SharePoint server, run following PowerShell in the SharePoint Server Management Shell:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
      
    #Variables
    $SiteUrl="https://webapplication/sites/testsite"
    $ListName="list"
    $OutPutFile = "C:\ListData.csv"
      
    #Get Web and List
    $web = Get-SPWeb $SiteUrl
    $List = $Web.Lists[$ListName]
    Write-host "Total Number of Items Found:"$List.Itemcount
     
    #Array to Hold Result - PSObjects
    $ListItemCollection = @()
       
     #Get All List items
     $List.Items | ForEach {
     write-host "Processing Item ID:"$_["ID"]
      
       $ExportItem = New-Object PSObject
       #Get Each field
       foreach($Field in $_.Fields)
        {
            $ExportItem | Add-Member -MemberType NoteProperty -name $Field.InternalName -value $_[$Field.InternalName] 
        }
        #Add the object with property to an Array
        $ListItemCollection += $ExportItem
     
    }   
    #Export the result Array to CSV file
    $ListItemCollection | Export-CSV $OutPutFile -NoTypeInformation
    Write-host -f Green "List '$ListName' Exported to $($OutputFile) for site $($SiteURL)"
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.