Update-List
Adds items to and removes items from a property value that contains a collection of objects.
Syntax
Update-List
[-Add <Object[]>]
[-Remove <Object[]>]
[-InputObject <PSObject>]
[[-Property] <String>]
[<CommonParameters>]
Update-List
-Replace <Object[]>
[-InputObject <PSObject>]
[[-Property] <String>]
[<CommonParameters>]
Description
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 do not specify a property in the command,
Update-List
returns an object that describes the
update instead of updating the object. You can submit the update object to cmdlets that change
objects, such as
Set
cmdlets.
This cmdlet works only when the property that is being updated supports the
IList
interface that
Update-List
uses. Also, any
Set
cmdlets that accept an update must support the
IList
interface.
The core cmdlets that are installed with PowerShell do not support this interface. To
determine whether a cmdlet supports
Update-List
, see the cmdlet Help topic.
This cmdlet was reintroduced in PowerShell 7.
Examples
Example 1: Add items to a property value
In this example we create a class that represents a deck of cards where the cards are stored as a
List
collection object. The
NewDeck()
method uses
Update-List
to add a complete deck of
card values to the
cards
collection.
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 = "`u{2663}","`u{2666}","`u{2665}","`u{2660}"
$_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() }
}
Note
The
Update-List
cmdlet outputs the updated object to the pipeline. We pipe the output to
Out-Null
to suppress the unwanted display.
Example 2: Add and remove items of a collection property
Continuing with the code in Example 1, we will create instances of the
Cards
class to represent
a deck of cards and the cards held by two players. We use the
Update-List
cmdlet to add cards to
the players' hands and to remove cards from the deck.
$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()
Deck : 4⦠7⥠J⦠5⣠A⣠8⦠J⣠Q⥠6⦠3⦠9⦠6⣠2â£
K⥠4â 10⥠8â 10⦠9â 6â K⦠7⣠3⣠Q⣠A⥠Qâ
3⥠5⥠2⦠5â J⥠Jâ 10⣠4⥠Q⦠10â 4⣠2â 2â¥
6⥠7⦠Aâ 5⦠8⣠9⥠Kâ 7â 3â 9⣠A⦠K⣠8â¥
Player 1 : 4⦠J⦠A⣠J⣠6â¦
Player 2 : 7⥠5⣠8⦠Q⥠3â¦
Deck : 9⦠6⣠2⣠K⥠4â 10⥠8â 10⦠9â 6â K⦠7⣠3â£
Q⣠A⥠Qâ 3⥠5⥠2⦠5â J⥠Jâ 10⣠4⥠Q⦠10â
4⣠2â 2⥠6⥠7⦠Aâ 5⦠8⣠9⥠Kâ 7â 3â 9â£
A⦠K⣠8â¥
The output shows the state of the deck before the cards were dealt to the players. You can see that
each player received five cards from the deck. The final output shows the state of the deck after
dealing the cards to the players.
Update-List
was used to select the cards from the deck and add
them to the players' collection. Then the players' cards were removed from the deck using
Update-List
.
Example 3: Add and remove items in a single command
Update-List
allows you to use the
Add
and
Remove
parameters in a single command. In this
example, Player 1 wants to discard the
4â¦
and
6â¦
and get two new cards.
# 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()
Player 1 : J⦠A⣠J⣠9⦠6â£
Deck : 2⣠K⥠4â 10⥠8â 10⦠9â 6â K⦠7⣠3⣠Q⣠Aâ¥
Qâ 3⥠5⥠2⦠5â J⥠Jâ 10⣠4⥠Q⦠10â 4⣠2â
2⥠6⥠7⦠Aâ 5⦠8⣠9⥠Kâ 7â 3â 9⣠A⦠Kâ£
8â¥
Parameters
-Add
Specifies the property values to be added to the collection. Enter the values in the order that they should appear in the collection.
Type: | Object [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-InputObject
Specifies the objects to be updated. You can also pipe the object to be updated to
Update-List
.
Type: | PSObject |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-Property
Specifies the property that contains the collection that is being updated. If you omit this
parameter,
Update-List
returns an object that represents the change instead of changing the
object.
Type: | String |
Position: | 0 |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Remove
Specifies the property values to be removed from the collection.
Type: | Object [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Replace
Specifies a new collection. This parameter replaces all items in the original collection with the items specified by this parameter.
Type: | Object [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
PSObject
You can pipe the object to be updated to this cmdlet.
Outputs
PSListModifier
By default, this cmdlet returns an object representing the update action.
Object
When you specify the Property parameter, this cmdlet returns the updated object.