Last active
June 2, 2021 12:54
-
-
Save ktskumar/ee49c1902e57fbd6a6fa8c0e8de71a05 to your computer and use it in GitHub Desktop.
Export all list data from a SharePoint Site using Microsoft 365 CLI
This file contains hidden or 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
#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