# Remove a value from an array

**URL:** <https://forums.powershell.org/t/remove-a-value-from-an-array/13012>\
**Category:** PowerShell Help\
**Created:** [August 17, 2019, 10:26am UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012 "2019-08-17T10:26:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgoel](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@mgoel](https://forums.powershell.org/u/mgoel)\
**Post date:** [August 17, 2019, 10:26am UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/1 "2019-08-17T10:26:57Z")

</div>

I have a case, where I want to remove a value (at index number 2) from an array and at the same time, I am converting weblinks in the array to the file names. I am using the following code, but it is not working. What I am doing wrong?

&nbsp;

```
[string[]] $FilePath1 = @()
[string] $Filepath
for ($i=0; $i -le 534 ; $i++) {
if($i=2) {$i++}
$FilePath = 'C:\Test\' + $AllLinksNet[$i].Substring($AllLinksNet[$i].LastIndexOf('/')+1)
$FilePath1 += $FilePath
}
```

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [August 17, 2019, 11:09am UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/2 "2019-08-17T11:09:21Z")

</div>

From [Practical guide to PowerShell Arrays | Sam's Corner](https://superwidgets.wordpress.com/2018/01/01/practical-guide-to-powershell-arrays/)  
"To remove the 4th and 8th array elements for example regardless of their values, we use an expression similar to:  
$c = 0…($c.Length-1) | foreach { if ($PSItem -notin 3,7) { $c[$PSItem] } }  
"  
In your case, to remove the 2nd array element of the $FilePath1 array:

```
$FilePath1 = 0..($FilePath1.Length-1) | foreach { if ($PSItem -ne 1) { $FilePath1[$PSItem] } }
```

Notice that you’re re-declaring the array…

---

<div class="post-metadata">

**Author:** ![mgoel](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@mgoel](https://forums.powershell.org/u/mgoel)\
**Post date:** [August 17, 2019, 11:57am UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/3 "2019-08-17T11:57:32Z")

</div>

Thank you. It worked. But for my own knowledge, can you please point out the mistake in my code:

```
[string[]] $FilePath1 = @()
[string] $Filepath
for ($i=0; $i -le 534 ; $i++) {
if($i=2) {$i++}
$FilePath = 'C:\Test\' + $AllLinksNet[$i].Substring($AllLinksNet[$i].LastIndexOf('/')+1)
$FilePath1 += $FilePath
}
```

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [August 17, 2019, 6:53pm UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/4 "2019-08-17T18:53:27Z")

</div>

```
$PathList = for ($i=0; $i -le 534 ; $i++) {
    if ($i -ne 2) {
        'C:\Test\' + $AllLinksNet[$i].Substring($AllLinksNet[$i].LastIndexOf('/')+1)
    }
}
```

Use -eq instead of = in line 4  
Simplify code as shown

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [August 17, 2019, 11:37pm UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/5 "2019-08-17T23:37:39Z")

</div>

It would be so easy with an arraylist.

```
PS /Users/js> $a = 1,2,3
PS /Users/js> $a = [collections.arraylist]$a
PS /Users/js> $a.removeat(1)
PS /Users/js> $a
1
3
```

---

<div class="post-metadata">

**Author:** ![mgoel](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@mgoel](https://forums.powershell.org/u/mgoel)\
**Post date:** [August 20, 2019, 9:00am UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/6 "2019-08-20T09:00:26Z")

</div>

Thank you! Can you provide me a link where I can get more information about powershell Arraylist and how is different fro an Array. In which circumstances, which is preferable?

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [August 20, 2019, 9:05am UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/7 "2019-08-20T09:05:05Z")

</div>

Here’s the docs in terms of .net and C#, but you can easily translate it to powershell. It’s good if you want to keep adding and subtracting individual elements. [ArrayList Class (System.Collections) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8) For example, $error is an arraylist. And $error[0] is always the most recent one.

---

<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:32pm UTC](https://forums.powershell.org/t/remove-a-value-from-an-array/13012/8 "2024-05-16T20:32:41Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


