Forum Discussion

Alan2022's avatar
Alan2022
Iron Contributor
Jan 19, 2023

Saving sharepoint list data to excel.

Hi Community,

 

Sharing is good. (Requires PnP.PowerShell &  ImportExcel)

 

 

#Use powershell to save data from specific sharepoint list to excel
Cls
 
#Parameters
$SiteURL = "https://<company>.sharepoint.com/sites/<sitename>"
$ListName = "VM Header List"
$SelectedFields = @("Classification","VMName", "IPAddress")
$ExcelPath = "$($env:USERPROFILE)\Desktop\Logs\VMHeaderList.xlsx"
 
# User Authentication
$ua = @(Get-StoredCredential -Target UserAccount)
if($ua.count -eq 0){
    Write-Host "Enter UserName & Password..." 
    Exit    
}
$credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $ua.UserName,$ua.Password
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credential $credential
 
#Get List items from the list
$ListItems = Get-PnPListItem -List $ListName -Fields $SelectedFields -PageSize 2000
 
#Iterate through each item and extract data
$ListDataColl = @()
$ListItems | ForEach-Object {
    $ListData = New-Object PSObject
    #Get the Field Values of the item as text
    $ListItem  = Get-PnPProperty -ClientObject $_ -Property FieldValuesAsText
    ForEach($Field in $SelectedFields)
    {
        $ListData | Add-Member Noteproperty $Field $ListItem[$Field]
    }
    $ListDataColl += $ListData 
}

# Generate Report
if (Test-Path $ExcelPath) {
  Remove-Item $ExcelPath
}
$ListDataColl
$ListDataColl | Export-Excel $ExcelPath -AutoSize -TableName VMHeaderList 
Write-Host "Finished..." 

 

 

 

No RepliesBe the first to reply

Resources