CmdID / PowerShell / Cmdlets / Update
Update-List
Adds items to and removes items from a property value that contains a collection of objects. PowerShell reference
From the PowerShell reference 5 parameters
What it does
Adds items to and removes items from a property value that contains a collection of objects. The Update-List cmdlet adds, removes, or replaces items in a property value of an object and returns the updated object. This cmdlet is designed for properties that contain collections of objects. The Add and Remove parameters add individual items to and remove them from the collection. The Replace parameter replaces the entire collection. If you don't specify a property in the command, Update-List returns a hashtable that describes the update instead of updating the object. Later, you can use this change set to update a list object. This cmdlet works only when the property that's being updated supports the IList interface that Update-List uses. Also, any Set cmdlets that accept an update must support the IList interface.
Update-List [-Add <Object[]>] [-Remove <Object[]>] [-InputObject <PSObject>] [[-Property] <String>] [<CommonParameters>]Update-List -Replace <Object[]> [-InputObject <PSObject>] [[-Property] <String>] [<CommonParameters>]
Add items to a property value
class Cards {
[System.Collections.Generic.List[string]]$Cards
[string]$Name
Cards([string]$_name) {
$this.Name = $_name
$this.Cards = [System.Collections.Generic.List[string]]::new()
}
NewDeck() {
$_suits = [char]0x2663,[char]0x2666,[char]0x2665,[char]0x2660
$_values = 'A',2,3,4,5,6,7,8,9,10,'J','Q','K'
$_deck = foreach ($s in $_suits){ foreach ($v in $_values){ "$v$s"} }
$this | Update-List -Property Cards -Add $_deck | Out-Null
}
Show() {
Write-Host
Write-Host $this.Name ": " $this.Cards[0..12]
if ($this.Cards.Count -gt 13) {
Write-Host (' ' * ($this.Name.Length+3)) $this.Cards[13..25]
}
if ($this.Cards.Count -gt 26) {
Write-Host (' ' * ($this.Name.Length+3)) $this.Cards[26..38]
}
if ($this.Cards.Count -gt 39) {
Write-Host (' ' * ($this.Name.Length+3)) $this.Cards[39..51]
}
}
Shuffle() { $this.Cards = Get-Random -InputObject $this.Cards -Count 52 }
Sort() { $this.Cards.Sort() }
}IdentifyTrips 1 Sigma rule: highPotential PowerShell Obfuscation Via WCHAR/CHAR by Florian Roth (Nextron Systems)
Add and remove items of a collection property
$player1 = [Cards]::new('Player 1')
$player2 = [Cards]::new('Player 2')
$deck = [Cards]::new('Deck')
$deck.NewDeck()
$deck.Shuffle()
$deck.Show()
# Deal two hands
$player1 | Update-List -Property Cards -Add $deck.Cards[0,2,4,6,8] | Out-Null
$player2 | Update-List -Property Cards -Add $deck.Cards[1,3,5,7,9] | Out-Null
$deck | Update-List -Property Cards -Remove $player1.Cards | Out-Null
$deck | Update-List -Property Cards -Remove $player2.Cards | Out-Null
$player1.Show()
$player2.Show()
$deck.Show()IdentifyAdd and remove items in a single command
# Player 1 wants two new cards - remove 2 cards & add 2 cards
$player1 | Update-List -Property Cards -Remove $player1.Cards[0,4] -Add $deck.Cards[0..1] | Out-Null
$player1.Show()
# remove dealt cards from deck
$deck | Update-List -Property Cards -Remove $deck.Cards[0..1] | Out-Null
$deck.Show()IdentifyApply a change set to a list object
$list = [System.Collections.ArrayList] (1, 43, 2)
$changeInstructions = Update-List -Remove 43 -Add 42
$changeInstructionsIdentifyIts page in the reference: Update-List.
Parameters
-AddSpecifies the property values to be added to the collection. Enter the values in the order that they should appear in the collection.-InputObjectSpecifies the objects to be updated. You can also pipe the object to be updated to Update-List.-PropertySpecifies the property that contains the collection that's being updated. If you omit this parameter, Update-List returns an object that represents the change instead of changing the object.-RemoveSpecifies the property values to be removed from the collection.-ReplaceSpecifies a new collection. This parameter replaces all items in the original collection with the items specified by this parameter.