# Array remove element

**URL:** <https://forums.powershell.org/t/array-remove-element/2036>\
**Category:** PowerShell Help\
**Created:** [November 6, 2013, 7:21am UTC](https://forums.powershell.org/t/array-remove-element/2036 "2013-11-06T07:21:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wantto-wantto](https://avatars.discourse-cdn.com/v4/letter/w/c37758/32.png) [@wantto-wantto](https://forums.powershell.org/u/wantto-wantto)\
**Post date:** [November 6, 2013, 7:21am UTC](https://forums.powershell.org/t/array-remove-element/2036/1 "2013-11-06T07:21:47Z")

</div>

i have array :

Clear-Host;  
$arr=@();  
Add-Type @’  
public class MyObject2  
{  
public int MyField = 5;  
public void xTimesMyField(int x) {  
MyField=x;  
}  
}  
'@  
for( $i=1;$i -le 10;++$i){  
$object = New-Object MyObject2;  
$object.xTimesMyField($i);  
$arr+=$object;  
}  
$arr

how can i remove one of the elements of the array by theMyField property when it equales to certain value?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 6, 2013, 7:23am UTC](https://forums.powershell.org/t/array-remove-element/2036/2 "2013-11-06T07:23:54Z")

</div>

You can’t remove elements from a .NET Array object. You can either use another type of collection (such as an ArrayList), or you can filter the elements and assign them to a new array object (even if it’s in the same variable.) For example:

```
$arr = $arr | Where-Object { $_.MyField -ne 'SomeValue' }
```

---

<div class="post-metadata">

**Author:** ![wantto-wantto](https://avatars.discourse-cdn.com/v4/letter/w/c37758/32.png) [@wantto-wantto](https://forums.powershell.org/u/wantto-wantto)\
**Post date:** [November 6, 2013, 7:56am UTC](https://forums.powershell.org/t/array-remove-element/2036/3 "2013-11-06T07:56:54Z")

</div>

thanks

---

<div class="post-metadata">

**Author:** ![zachneal](https://avatars.discourse-cdn.com/v4/letter/z/5daacb/32.png) [@zachneal](https://forums.powershell.org/u/zachneal)\
**Post date:** [November 8, 2013, 6:58am UTC](https://forums.powershell.org/t/array-remove-element/2036/4 "2013-11-08T06:58:46Z")

</div>

You can do some funky stuff with multiple assignment and recursion to remove an element.

Removing based on what the element is.

```
`
function remove-item-list ($item,[array]$chckd_list=@(),[array]$list=(throw"the item $item was not in the list"))
{ if ($list.length -lt 1 ) { throw "the item $item was not in the list" }

$check\_item,$temp\_list=$list  
if ($check\_item -eq $item ) {  
$chckd\_list+=$temp\_list  
return $chckd\_list }  
else {  
$chckd\_list+=$check\_item  
return (remove-item-list -item $item -chckd\_list $chckd\_list -list $temp\_list )}  
}  
`
```

Removing based on index number.

```
`
function remove-index-list ([int]$count=0,[int]$index,[array]$chckd_list=@(),[array]$list)
{if (($list.length+$count-1) -lt $index ) { throw "the index is out of range" }
$check_item,$temp_list=$list
if ($count -eq $index) {
$chckd_list+=$temp_list
return $chckd_list}
else {
$chckd_list+=$check_item
return (remove-index-list -count ($count + 1) -index $index -chckd_list $chckd_list -list $temp_list )}

}  
`
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:56pm UTC](https://forums.powershell.org/t/array-remove-element/2036/5 "2024-05-16T20:56:55Z")

</div>


