PoshBytes: Update-List
The often forgotten, but always useful Update-List.
This post is a companion for the video embedded below. Scroll down to see the code from the video.
Create Sample Objects
Define a helper function that returns a test server object with a Tags list, then create two copies for side-by-side comparisons.
Function Get-TestSrv {
param($n = 1)
[pscustomobject]@{
Name = "SRV0$n"
Tags = [Collections.Generic.List[string]]('dev', 'windows')
}
}
$clean = Get-TestSrv
$clunky = Get-TestSrv
$clean
$clunky
Add Items the Manual Way
Use .Add() directly on the list to append new tags.
$clunky.Tags.Add('prod')
$clunky.Tags.Add('sql')
$clunky
Add Items with Update-List
Add multiple items to the list property in a single, declarative call.
$clean | Update-List -Property Tags -Add 'prod', 'sql'
Remove Items the Manual Way
Remove specific tags by calling .Remove() on the list for each item.
$clunky.Tags.Remove('dev')
$clunky.Tags.Remove('sql')
$clunky
Remove Items with Update-List
Remove multiple tags from the list property in one command.
$clean | Update-List -Property Tags -Remove 'dev', 'sql'
Mix Remove and Add the Manual Way
Manually remove one tag and add another to reshape the list.
$clunky.Tags.Remove('windows')
$clunky.Tags.Add('linux')
$clunky
Mix Remove and Add with Update-List
Perform both removal and addition in a single, readable command.
$clean | Update-List -Property Tags -Remove 'windows' -Add 'linux'
Replace the Entire List Manually
Overwrite the property with a brand new list instance containing the target values.
$clunky.Tags = [Collections.Generic.List[string]]('critical', 'linux', 'sql')
$clunky
Replace the Entire List with Update-List
Use -Replace to set the list property to an exact set of values in one step.
$clean | Update-List -Property Tags -Replace 'critical', 'linux', 'sql'
Create Multiple Servers
Prepare two sets of server objects for batch updates and comparisons.
$cleanServers = 1..3 | ForEach-Object { Get-TestSrv $_ }
$clunkyServers = 1..3 | ForEach-Object { Get-TestSrv $_ }
$clunkyServers
Batch Update Manually with a Loop
Iterate all objects and call .Add() on each list property.
$clunkyServers | ForEach-Object {
$_.Tags.Add('monitored')
}
$clunkyServers
Batch Update with Update-List
Pipe the collection into Update-List to modify every object’s list property in one pipeline.
$cleanServers | Update-List -Property Tags -Add 'monitored'
Pitfall of Using + on a List Property
Using + forces an array operation that changes the property type away from a list.
$clunky.Tags = $clunky.Tags + 'oops'
$clunky.Tags.GetType().FullName
Manual Repair Back to List
Rebuild the list by copying items into a new List[string] and assign it back.
$tmp = [Collections.Generic.List[string]]::new()
$tmp.AddRange([string[]]$clunky.Tags)
$tmp.Add('fixed')
$clunky.Tags = $tmp
$clunky.Tags.GetType().FullName
Update-List Preserves the List Type
Adding with Update-List keeps the property as a list without converting it to an array.
$clean | Update-List -Property Tags -Add 'woot'
$clean.Tags.GetType().FullName
Capturing a Delta and Applying It
Use Update-List to build a reusable delta object, then apply it to an existing list.
$delta = Update-List -Add 'gpu' -Remove 'linux'
([pslistmodifier]$delta).ApplyTo($clean.Tags)
$clean.Tags
- Update-List edits list properties in place: -Add, -Remove, -Replace.
- Pipeline-friendly; add and remove together in one call.
- Can emit a change set [pslistmodifier] to apply later.
- Avoids verbose read–modify–reassign patterns with = / +=.
- Reintroduced in PowerShell 7+;
- Works with any property that supports IList.