Recently I needed to perform some basic cleanup and syncing operation in one of the SharePoint online site. In process I also need to export differnces or changes I have made to keep in a CSV. I have not found any specific documentation about doing so in PowerShell. I have put a sample script here to find such details and generate a CSV. Here I have used connection to a SharePoint list.
#Setup Credentials to connect
Write-Host -ForegroundColor Yellow "Starting utility to sync data on instance lists from template list..." (get-date).ToString('T')
$siteUrl = "https://yourserver/sites/ListABC"
$instanceListFields = "Id","Title", "Description"
$applicationRegsitryList = "List For All List Titles";
$applicationRegsitryListFields = "Id","Title"
Write-Host -ForegroundColor Yellow "Connecting to Sharepoint online services to site... " $siteUrl
Connect-PnPOnline -Url $siteUrl -UsewebLogin
Write-Host -ForegroundColor Yellow "Connected to Sharepoint online services successfully."
New-Object -TypeName System.Collections.ArrayList
$finalMessage = [System.Collections.Arraylist]@()
$clientContext = Get-PnPContext
$targetWeb = Get-PnPWeb
$countOfListsUpdated = 0;
foreach($appListItem in $applicationRegsitryList)
{
try {
$instanceListItems = Get-PnPListItem -List $appListItem["Title"] -Fields $instanceListFields -ErrorAction Stop
Write-Host "Got list : "$appListItem["Title"]" Item count: "$instanceListItems.Count
$actionStatus = "Got list : " + $appListItem["Title"] + " Item count: " + $instanceListItems.Count
$message = [System.Collections.Arraylist]@()
$message.Add("Found");
$message.Add($appListItem["Title"]);
$message.Add($actionStatus);
$message.Add($appListItem["ID"]);
$finalMessage.Add($message);
}
catch {
Write-Host -ForegroundColor Red "Exception Occurred... while fetching list from title"
$errormsg = $_.ToString();
$exception = $_.Exception;
$actionStatus = "Could not fetch list with Title " + $errormsg + " " + $exception;
$message = [System.Collections.Arraylist]@()
$message.Add("Not Found");
$message.Add($appListItem["Title"]);
$message.Add($actionStatus);
$message.Add($appListItem["ID"]);
$finalMessage.Add($message);
}
}
$holder = @()
$pName = @("Status", "ListName", "Message", "ID")
$num = 4
FOREACH($row IN $finalMessage){
$obj = New-Object PSObject
for($i=0;$i -lt $num ; $i++){
$obj | Add-Member -MemberType NoteProperty -name $pName[$i] -Value $row[$i]
}
$holder += $obj
$obj = $null
}
$currTime = Get-Date -UFormat %R;
$excelFileName = "DiffChecker_" + (get-date).ToString('d_M_y') + "_" + $currTime.Replace(":", "") + ".csv"
$holder | Export-Csv $excelFileName -NoTypeInformation
Write-Host -ForegroundColor Yellow "Completed utility to find list for each title. Total lists processed: " $countOfListsUpdated " " (get-date).ToString('T')