Skip to content

Instantly share code, notes, and snippets.

@ktskumar
Last active June 2, 2021 12:54
Show Gist options
  • Save ktskumar/ee49c1902e57fbd6a6fa8c0e8de71a05 to your computer and use it in GitHub Desktop.
Save ktskumar/ee49c1902e57fbd6a6fa8c0e8de71a05 to your computer and use it in GitHub Desktop.
Export all list data from a SharePoint Site using Microsoft 365 CLI
#Export all lists properties (Specified in $listProperties variable) from the SharePoint in to CSV format
###
$outputfilepath = "<PUTYOURPATHHERE.csv>"
$weburl = "https://contoso.sharepoint.com/"
# Provide List Properties in below variable
$listProperties = @('Title', 'Url', 'Id')
###
Write-host 'ensure logged in'
$m365Status = m365 status
if ($m365Status -eq "Logged Out") {
m365 login
}
###
Write-Host 'Get all lists from web: ' $weburl
$allLists = m365 spo list list --webUrl $weburl -o json | ConvertFrom-Json
###
# Convert JSON to CSV Object
$listValues = @()
foreach ($singleList in $allLists) {
$listObject = New-Object -TypeName PSObject
foreach ($prop in $listProperties) {
$listObject | Add-Member -MemberType NoteProperty -Name $prop -Value $singleList.$prop
}
$listValues += $listObject
}
###
Write-Host 'Exports all the list properties (specified in $listProperties) to: ' $outputfilepath
$listValues | Export-Csv -Path $outputfilepath -NoTypeInformation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment