Remove a value from an array

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?

 

 [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
}

From 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…

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
}
$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

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

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?

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. 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.