Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Remove item [0] from an array

I'm struggling a bit to remove the first line (item ID) of an array.

$test.GetType()  IsPublic IsSerial Name                                     BaseType                                                                                                       -------- -------- ----                                     --------                                                                                                       True     True     Object[]                                 System.Array 

To list all the options I tried ,$test | gm and it clearly states:

Remove         Method                void IList.Remove(System.Object value)                                                                                               RemoveAt       Method                void IList.RemoveAt(int index) 

So when I try $test.RemoveAt(0) I get the error:

Exception calling "RemoveAt" with "1" argument(s): "Collection was of a fixed size."At line:1 char:1 + $test.RemoveAt(1) + ~~~~~~~~~~~~~~~~~     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException     + FullyQualifiedErrorId : NotSupportedException 

So I finally found here that my array needs to be of the type System.Object to be able to use $test.RemoveAt(0). Is it best practice to declare all the arrays in the beginning of the script as a list? Or is it better to convert the arrays with $collection = ({$test}.Invoke()) to a list later on when this functionality is needed?

What are the pro's and cons of both types? Thank you for your help.

like image 379
DarkLite1 Avatar asked Jul 15 '14 09:07

DarkLite1


People also ask

How do I Remove values from an array in PowerShell?

RemoveAt: Remove Objects by Index It was easy to find the index of “C” in such a small array list, but it you have a larger array list, you can use the IndexOf method to find the index and then use the RemoveAt method to remove the object at that index.

How do I Remove a specific element from an array?

pop() function: This method is use to remove elements from the end of an array. shift() function: This method is use to remove elements from the start of an array. splice() function: This method is use to remove elements from the specific index of an array.

How do I Remove an item from an array by value?

To remove an object from an array by its value:Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.

How do I get the first element of an array in PowerShell?

Windows PowerShell arrays are zero-based, so to refer to the first element of the array $var3 (“element zero”), you would write $var3 [0].


2 Answers

An alternative option is to use Powershell's ability to assign multiple variables (see this other answer).

$arr = 1..5 $first, $rest= $arr  $rest 2 3 4 5 

It's been a feature of Powershell for over a decade. I found this functionality from an MSDN blog post:

like image 135
Henry Ward Avatar answered Sep 18 '22 23:09

Henry Ward


Arrays are fixed-size, like the error says. RemoveAt() is an inherited method that doesn't apply to normal arrays. To remove the first entry in the array, you could overwrite the array by a copy that includes every item except the first, like this:

$arr = 1..5  $arr 1 2 3 4 5  $arr = $arr[1..($arr.Length-1)]  $arr 2 3 4 5 

If you need to remove values at different indexes then you should consider using a List. It supports Add(), Remove() and RemoveAt():

#If you only have a specific type of objects, like int, string etc. then you should edit `[System.Object] to [System.String], [int] etc. $list = [System.Collections.Generic.List[System.Object]](1..5)  $list 1 2 3 4 5  $list.RemoveAt(0)  $list 2 3 4 5 

See my earlier SO answer and about_Arrays for more details about how arrays work.

like image 42
Frode F. Avatar answered Sep 22 '22 23:09

Frode F.