In SharePoint, the List is a collection of content that has rows (List Items) and columns (Item fields). The list can be a Generic List that is used to store generic information and a Document library list that is used to store files. In this blog, we will explore how to retrieve SharePoint Online list items using PnP PowerShell and export the result to a CSV file.
Before you start, run the following command to install the latest PnP PowerShell module if you have not already installed it.
Install-Module -Name "PnP.PowerShell"
We can use the Get-PnPListItem cmdlet to get all list items from a SharePoint Online Site. The below commands retrieve the list items from the given list.
# Provide Site URL and List name
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ListName = "ProductList"
Try {
# Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
# Get List Items
$ListItems= (Get-PnPListItem -List $ListName -Fields "Title","ProductName","ProductVersion")
ForEach($ListItem in $ListItems)
{
Write-Host "Title" : $ListItem["Title"]
Write-Host "ProductName" : $ListItem["ProductName"]
Write-Host "ProductVersion" : $ListItem["ProductVersion"]
Write-Host "#####################################"
}
}
catch {
Write-host "Error occured: $_" -f Red
}
Export SharePoint Online List Items to CSV file
The following commands fetch the list items from the given list and export the details to a CSV file.
# Provide Site URL and List name
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
$ListName = "ProductList"
# Connect to PnP Online
Connect-PnPOnline -Url $SiteURL
$Result = @()
# Get List Items
$ListItems= (Get-PnPListItem -List $ListName -Fields "Title","ProductName","ProductVersion")
ForEach($ListItem in $ListItems)
{
$Result += New-Object PSObject -property $([ordered]@{
Title = $ListItem["Title"]
ProductName = $ListItem["ProductName"]
ProductVersion = $ListItem["ProductVersion"]
})
}
# Export the result to CSV
$Result | Export-CSV "C:\SPOListItems.CSV" -NoTypeInformation -Encoding UTF8
Advertisement