Last active
August 29, 2021 20:31
-
-
Save Zerg00s/1732c0c2ae05459731a6442b8ccef4bc to your computer and use it in GitHub Desktop.
PnP.PowerShell - Export List to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Install-Module -Name PnP.PowerShell -Force -Scope CurrentUser | |
Connect-PnPOnline https://drps.sharepoint.com -UseWebLogin | |
$ListTitle = 'News Room' | |
$list = Get-PnPList $ListTitle -Includes Fields | |
$items = Get-PnPListItem -List $list -PageSize 100 | |
$fields = $list.Fields | Where-Object { | |
( | |
$_.InternalName -eq "ID" -or | |
$_.InternalName -eq "Created" -or | |
$_.InternalName -eq "Modified" -or | |
$_.InternalName -eq "Author" -or | |
$_.InternalName -eq "Editor" | |
)-or | |
( | |
$_.Hidden -eq $false -and | |
$_.FieldTypeKind -ne "Computed" -and | |
$_.Hidden -eq $false -and | |
$_.ReadOnlyField -eq $false -and | |
$_.Title -ne "Attachments" | |
) | |
} | |
$rows = $items | |
$convertedRows = @() | |
foreach ($item in $rows){ | |
$convertedRow = New-Object PSObject | |
foreach ($field in $fields){ | |
if($field.TypeAsString -eq "User" -or $field.TypeAsString -eq "Lookup"){ | |
$convertedRow | Add-Member -MemberType NoteProperty -name $field.Title -value $item[$field.InternalName].LookupValue | |
}elseif ($field.TypeAsString -eq "UserMulti"){ | |
# TODO: not handled yet | |
}else{ | |
$convertedRow | Add-Member -MemberType NoteProperty -name $field.Title -value $item[$field.InternalName] | |
} | |
} | |
$convertedRows += $convertedRow | |
} | |
# The generated CSV file is not well formatted. Need to work more on it. | |
$convertedRows | Export-CSV "C:\Temp\items.csv" -NoTypeInformation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment