Powershell : Retrieve sharepoint list data

venkatesh padmanabhan 181 Reputation points
2022-04-01T08:40:50.75+00:00

Hi.
I am trying to export the SharePoint list data to CSV by following this link :

https://www.sharepointdiary.com/2013/04/export-sharepoint-list-items-to-csv-using-powershell.html

I am following the last example given. i am trying the code as :

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Variables

$SiteUrl="sharepoint site"
$ListName="list name"
$OutPutFile = "C:\path\ListData.csv"

Delete the Output File if exists

If (Test-Path $OutPutFile) { Remove-Item $OutPutFile }

Get Web and List Objects

$Web = Get-SPWeb $SiteUrl
$List = $Web.Lists[$ListName]
Write-host "Total Number of Items Found:"$List.Itemcount

I am getting error while running in powershell as Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.

How to fix this ?
Thanks

Update 1: I have tried this :
PS C:\WINDOWS\system32> Install-Module -Name Microsoft.Online.SharePoint.PowerShell

PS C:\WINDOWS\system32>
Add-PSSnapin Microsoft.SharePoint.PowerShell
Add-PSSnapin : The Windows PowerShell snap-in 'Microsoft.SharePoint.PowerShell' is not installed on this computer.
At line:2 char:1

  • Add-PSSnapin Microsoft.SharePoint.PowerShell
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (Microsoft.SharePoint.PowerShell:String) [Add-PSSnapin], PSArgumentException
  • FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

I do not see a folder named Microsoft.SharePoint.Powershell in the C:\Windows\Microsoft.NET\assembly\GAC_MSIL path.
Can powershell be run from client machine, where sharepoint is not installed ?

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,542 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,180 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. sadomovalex 3,621 Reputation points
    2022-04-01T15:44:33.777+00:00

    those example which you provided uses server object model PowerShell cmdlets - they work only with Sharepoint on-prem (not with Sharepoint online) and only when this PowerShell runs on the server which is part of Sharepoint farm.

    1 person found this answer helpful.
    0 comments No comments

  2. Echo Du_MSFT 17,056 Reputation points
    2022-04-04T01:40:55.337+00:00

    Hi @venkatesh padmanabhan ,

    The code you provided is for SharePoint on-prem and not for SharePoint Online. It is recommended that you can run the following PowerShell script as an admin:

    Export SharePoint Online List Items to CSV using PowerShell

    #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  
    

    189545-1.jpg

    189514-2.jpg

    189562-3.jpg

    Reference:

    Thanks,
    Echo Du

    =========================================

    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.