Removing an element with a specific value from an array can be achieved in a number of different ways.
Again, we start with an array of 100 elements, as follows:
$oldArray = 1..100
The Where object may be used to identify and omit the element with the value 50. If 50 were to occur more than once, all instances would be omitted:
$newArray = $oldArray | Where-Object { $_ -ne 50 }
The index of the element might be identified and removed using the methods explored in removing elements according to the index:
$index = $oldArray.IndexOf(50)
If the value of the variable index is -1, the value is not present in the array (0 would indicate that it is the first element):
$index = $oldArray.IndexOf(50) if ($index -gt -1) { $newArray = $oldArray[0..($index - 1)] + $oldArray[($index + 1)..99] }
Unlike the Where object version, which inspects all elements, IndexOf gets the first occurrence of a value only. A complementary method, LastIndexOf, allows the most recent occurrence of a value to be removed.