We have office 365 and Shaepoint online, Want to download List as a CSV with Powershell, can somebody help me?

Aishwarya Apte 0 Reputation points
2023-10-17T14:11:37.3466667+00:00

What Package shall I use and What Modules do I need to Import ?

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
3,989 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,900 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,178 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Dillon Silzer 54,831 Reputation points
    2023-10-17T15:30:19.09+00:00

    Hello,

    You can use the following code:

    #Config Parameter  
    $SiteURL = "https://domain.sharepoint.com/sites/sitename"  
    $ListName = "listname"  
    #InternalName of the selected fields  
    $SelectedFields = @("ID","Title","Choice1","Person1","Date1")   
    $CSVPath = "C:\Temp\ListData.csv"  
    $ListDataCollection= @()  
       
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)  
    $Counter = 0  
      
    #PageSize:The number of items to retrieve per page request  
    $ListItems = Get-PnPListItem -List $ListName -Fields $SelectedFields -PageSize 2000  
       
    #Get all items from list  
    $ListItems | ForEach-Object {  
            $ListItem  = Get-PnPProperty -ClientObject $_ -Property FieldValuesAsText  
            $ListRow = New-Object PSObject  
            $Counter++  
            ForEach($Field in $SelectedFields)   
            {  
                $ListRow | Add-Member -MemberType NoteProperty $Field $ListItem[$Field]  
            }  
            Write-Progress -PercentComplete ($Counter / $($ListItems.Count)  * 100) -Activity "Exporting List Items..." -Status  "Exporting Item $Counter of $($ListItems.Count)"  
            $ListDataCollection += $ListRow  
    }  
    #Export the result Array to CSV file  
    $ListDataCollection | Export-CSV $CSVPath -NoTypeInformation  
    
    

    Cited from https://learn.microsoft.com/en-us/answers/questions/796617/powershell-retrieve-sharepoint-list-data


    If this is helpful please accept answer.


  2. Yanli Jiang - MSFT 22,036 Reputation points Microsoft Vendor
    2023-10-18T02:33:30.8866667+00:00

    Hi @Aishwarya Apte ,

    If you are sure that there is no problem with your account password and it has not expired, it is likely that MFA is enabled in the environment. You can try to use the following statement to connect:

    Connect-PnPOnline -Url $SiteURL -Interactive
    

    For more information, please refer to:

    https://www.sharepointdiary.com/2020/07/fix-sign-in-name-password-does-not-match-one-in-microsoft-account-system-error.html

    Moreover, according to your description, if it is a general list, you can also choose to directly use the built-in Export to CSV feature to export the list to a csv file.

    User's image

    Hope this helps.


    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.


  3. Yanli Jiang - MSFT 22,036 Reputation points Microsoft Vendor
    2023-10-23T09:27:23.69+00:00

    Hi @Aishwarya Apte ,

    The required permissions for the App Registration are Sites.ReadWrite.Alland User.Read.All.

    Regarding the issue with multiline description fields not exporting, you can try using the property FieldValuesAsHtml instead of FieldValuesAsText in the cmdlet Get-PnPListItem. This should export the multiline description field as HTML.

    For your reference:

    #Config Parameter  
    $SiteURL = "https://domain.sharepoint.com/sites/sitename"  
    $ListName = "listname"  
    #InternalName of the selected fields  
    $SelectedFields = @("ID","Title","Choice1","Person1","Date1", "Description")   
    $CSVPath = "C:\Temp\ListData.csv"  
    $ListDataCollection= @()  
       
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -AppId "YourAppId" -AppSecret "YourAppSecret" -Tenant "YourTenant.onmicrosoft.com"
    $Counter = 0  
      
    #PageSize:The number of items to retrieve per page request  
    $ListItems = Get-PnPListItem -List $ListName -Fields $SelectedFields -PageSize 2000  
       
    #Get all items from list  
    $ListItems | ForEach-Object {  
            $ListItem  = Get-PnPProperty -ClientObject $_ -Property FieldValuesAsHtml  
            $ListRow = New-Object PSObject  
            $Counter++  
            ForEach($Field in $SelectedFields)   
            {  
                $ListRow | Add-Member -MemberType NoteProperty $Field $ListItem[$Field]  
            }  
            Write-Progress -PercentComplete ($Counter / $($ListItems.Count)  * 100) -Activity "Exporting List Items..." -Status  "Exporting Item $Counter of $($ListItems.Count)"  
            $ListDataCollection += $ListRow  
    }  
    #Export the result Array to CSV file  
    $ListDataCollection | Export-CSV $CSVPath -NoTypeInformation
    

    Hope this helps.


    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.