PowerShell script to update SharePoint online list items
I had a SharePoint online list and I added a new column into it. And then I wanted to work with a data, which is close to be real. So I needed to generate the data and insert it into the list. For the tasks like this I like to use division by module to create semi-random data.
The list consisted of more than 5k records, so I added an index by the field "Technology" to update the list data by selecting technologies 1 by 1.
First, I initialized an array with unique Technologies values. And I also created a method to update the SharePoint List Item. So, I iterated through the array of the technologies and updated the list.
$url = "https://markimarta.sharepoint.com/sites/tech/Markets"
$listTitle = 'TestList'
Connect-PnPOnline -Url $url -Interactive
Set-PnPTraceLog -On -Level Debug
$technologies = @('Technology 1', 'Technology 2', 'Technology 3, 'Technology 4', 'Technology 5', 'Technology 6')
function UpdateTechnology($techname) {
$query = "<View><Query><Where><Eq><FieldRef Name='Technologie'/><Value Type='Text'>$techname</Value></Eq></Where></Query></View>"
$items = Get-PnPListItem -List $listTitle -Query $query
$quarter= "Q1_2024"
for($i = 0; $i -le $items.Count - 1; $i++) {
if($i % 4 -eq 0) {
$quarter= "Q1_2024"
}
if($i % 3 -eq 0) {
$quarter= "Q4_2023"
}
if ($i % 2 -eq 0 -and $i % 4 -ne 0) {
$quarter= "Q3_2023"
}
if ($i % 7 -eq 0) {
$quarter= "Q2_2023"
}
$items[$i]["CurrentQuarter"] = $quarter
$items[$i].Update()
}
}
# Iterate through array and call the update function
foreach($tech in $technologies) {
UpdateTechnology -techname $tech
}