Example: Remove an object in an array list by value of object property

  • 1 August 2017
  • Sean Huggans
Description: 

Example: Remove an object in an array list by index based on the value of the one of the object's properties

Language: 
PowerShell
$ObjectInformationStore = New-Object System.Collections.ArrayList
$ObjectStoreEntry = New-Object -TypeName PSObject
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppName -Value A
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppVersion -Value 1a
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppPublisher -Value 1b
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppDescription -Value 1c
$ObjectInformationStore.Add($ObjectStoreEntry) | Out-Null
$ObjectStoreEntry = New-Object -TypeName PSObject
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppName -Value B
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppVersion -Value 2a
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppPublisher -Value 2b
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppDescription -Value 2c
$ObjectInformationStore.Add($ObjectStoreEntry) | Out-Null
$ObjectStoreEntry = New-Object -TypeName PSObject
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppName -Value C
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppVersion -Value 3a
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppPublisher -Value 3b
$ObjectStoreEntry | Add-Member -MemberType NoteProperty -Name AppDescription -Value 3c
$ObjectInformationStore.Add($ObjectStoreEntry) | Out-Null
 
cls
write-host "Before:"
$ObjectInformationStore | Out-String
write-host " "
 
$ObjectInformationStore.RemoveAt($ObjectInformationStore.IndexOf($($ObjectInformationStore | Where-Object -property AppName -eq "B"))) | Out-Null
 
write-host "After:"
$ObjectInformationStore | Out-String
write-host " "
Note: that all applications posted here are posted for use, both commercial and non-commercial, free of charge, and as such are provided without warranty of any kind whatsoever. FMSCUG or any program's author are not responsible for any damages or shortcomings that result from usage of any of these applications.