{"id":8152,"date":"2014-11-18T06:00:00","date_gmt":"2014-11-18T14:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=8152"},"modified":"2014-11-19T09:31:09","modified_gmt":"2014-11-19T17:31:09","slug":"removing-objects-from-arrays-in-powershell","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/11\/18\/removing-objects-from-arrays-in-powershell\/","title":{"rendered":"Removing Objects from Arrays in PowerShell"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>A member of the <a href=\"https:\/\/www.facebook.com\/groups\/powershell\/\">PowerShell group on Facebook<\/a> asked how to delete an object from an array. It&#8217;s a simple question, but the answer isn&#8217;t very simple at all. It&#8217;s one of those beginner questions that has an advanced answer.<\/p>\n<p>When I first answered this question in <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847882.aspx\">about_Arrays<\/a>, I didn&#8217;t really know much about the subject and I used the (very good) answer that the team gave me. But that answer was incomplete, and while there are many good posts on the topic, none really cover the whole scope of pitfalls.<\/p>\n<p>[Be sure to start with about_Arrays. I&#8217;ll assume that you&#8217;ve read it and won&#8217;t repeat the basics of arrays that are described there.]<\/p>\n<p>&nbsp;<\/p>\n<h2>Why is removing difficult?<\/h2>\n<p>Removing objects from arrays should be simple, but the default collection type in Windows PowerShell, an object array (System.Object[]), has a fixed size. You can change objects in the array, but you can&#8217;t add or delete them, because that would change the size of the array.<\/p>\n<p>Let&#8217;s look at the problem. I\u2019ll create an array called <strong>$letters<\/strong> and save a few letters in the array.<\/p>\n<pre> <span style=\"color: #8b0000;\">$letters<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #ff0000;\">\"a\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"b\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"c\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"d\"<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Now, I\u2019ll try to remove the \u201cc\u201d from the array.<\/p>\n<pre>    <span style=\"color: #8b0000;\">$letters<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$letters<\/span> <span style=\"color: #0000ff;\">-<\/span> <span style=\"color: #ff0000;\">\"c\"<\/span><\/pre>\n<blockquote><p><span style=\"color: #ff0000; font-family: Lucida Console; font-size: small;\">Method invocation failed because [System.Object[]] does not contain a method named &#8216;op_Subtraction&#8217;.<br \/>\nAt line:1 char:2<br \/>\n+\u00a0 $letters = $letters &#8211; &#8220;c&#8221;<br \/>\n+\u00a0 ~~~~~~~~~~~~~~~~~~~~~~~~~<br \/>\n+ CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : InvalidOperation: (op_Subtraction:String) [], RuntimeException<br \/>\n+ FullyQualifiedErrorId : MethodNotFound<\/span><\/p><\/blockquote>\n<p>That didn&#8217;t work, because object arrays [System.Object[]) don&#8217;t have a subtraction (op_Subtraction) method. Let&#8217;s find the methods that System.Object[] supports.<\/p>\n<p>Typically, to get the type, methods, and properties of an object, you pipe it to the <b>Get-Member<\/b> cmdlet. But, when you pipe a collection of objects to Get-Member, Get-Member gets the members of objects in the collection. To get the members of the collection (not the objects that it contains), use its <b>InputObject<\/b> parameter.<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image_thumb7.png\" alt=\"image\" width=\"389\" height=\"493\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>This command reveals that object arrays, like the one in the $letters variable have a <strong>Remove<\/strong> method. Let&#8217;s try the <strong>Remove<\/strong> method.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image8.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image_thumb8.png\" alt=\"image\" width=\"611\" height=\"95\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>Remove fails, because object arrays are a fixed size.<\/p>\n<p>This is where beginners get stuck and even experienced users hit Google, Facebook, or a PowerShell forum for help.<\/p>\n<h4><\/h4>\n<h2>Create a new array<\/h2>\n<p>One way to remove an object from a fixed-size array is to create a new array that includes only selected objects from the original array. In this command, we use a ForEach loop go through every letter in the $letters array. We add all of the objects in $letters to the $newLetters array, except for &#8220;c&#8221;. To add objects to the new array, use the + or += operators; the statements are equivalent.<\/p>\n<pre>    <span style=\"color: #8b0000;\">$newLetters<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\"> @()\r\n    <\/span><span style=\"color: #0000ff;\">foreach<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$letter<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$letters<\/span><span style=\"color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$letter<\/span> <span style=\"color: #0000ff;\">-ne<\/span> <span style=\"color: #ff0000;\">\"c\"<\/span><span style=\"color: #000000;\">)\r\n        {\r\n            <\/span><span style=\"color: #008000;\">#$newLetters = $newLetters + $letter<\/span>\r\n            <span style=\"color: #8b0000;\">$newLetters<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$newLetters<\/span> <span style=\"color: #0000ff;\">+=<\/span> <span style=\"color: #8b0000;\">$letter<\/span><span style=\"color: #000000;\">\r\n        }\r\n    }<\/span><\/pre>\n<blockquote><p><span style=\"font-family: Lucida Console; font-size: small;\">PS C:\\&gt; $newLetters<br \/>\na<br \/>\nb<br \/>\nd<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Interestingly, when we use the + or += operators, Windows PowerShell actually creates a new array each time in the background, because you can\u2019t change the size of array. Sadly, there is no \u2013= operator. Also, in this very simple example, it\u2019s easier use the Where-Object cmdlet to filter out the &#8220;c&#8221;, but we can assume that you\u2019re using an array for a more complex task.<\/p>\n<blockquote>\n<pre><span style=\"color: #8b0000;\">$newLetters<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$letters<\/span> <span style=\"color: #0000ff;\">|<\/span> <span style=\"font-weight: bold; color: #0000ff;\">Where-Object<\/span><span style=\"color: #000000;\"> { <\/span><span style=\"color: #8b0000;\">$_<\/span><span style=\"color: #000000;\"> \u2013<\/span><span style=\"color: #696969;\">ne<\/span><span style=\"color: #000000;\"> \"c\" }<\/span><\/pre>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>You can also use array index notation to exclude items from the new array. In this command, we add the items at indexes 0, 1, and 3 from $letters to the $newLetters array, but we exclude the item at index 2.<\/p>\n<pre>    <span style=\"color: #8b0000;\">$newLetters<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$letters<\/span><span style=\"color: #000000;\">[<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #000000;\">3<\/span><span style=\"color: #000000;\">]<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>You can reuse the $letters variable name, but you are still creating a new $letters array that is independent of the original one.<\/p>\n<pre>    <span style=\"color: #8b0000;\">$letters<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$letters<\/span><span style=\"color: #000000;\">[<\/span><span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #000000;\">1<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #000000;\">3<\/span><span style=\"color: #000000;\">]<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Use an ArrayList<\/h2>\n<p>My favorite solution to the \u201ccollection was of a fixed size\u201d problem is to create an <strong>ArrayList<\/strong> (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\">System.Collections.ArrayList<\/a>), instead of the default object array (System.Object[]). ArrayLists behave like arrays, but unlike arrays, ArrayList objects don\u2019t have a fixed size.<\/p>\n<p>To create an array list, cast the variable:<\/p>\n<pre><span style=\"color: #000000;\">    [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #8b0000;\">$caps<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #ff0000;\">\"A\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"B\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"D\"<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Or, cast the objects. (Don\u2019t forget the parentheses.)<\/p>\n<pre>    <span style=\"color: #8b0000;\">$caps<\/span> <span style=\"color: #0000ff;\">=<\/span><span style=\"color: #000000;\"> [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">](<\/span><span style=\"color: #ff0000;\">\"A\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"B\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"D\")<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Use the InputObject parameter of Get-Member to get the properties and methods of an array list. There\u2019s a <strong>Remove<\/strong> method of ArrayList objects and this one works.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image9.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image_thumb9.png\" alt=\"image\" width=\"534\" height=\"488\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2>Remove objects from an array list<\/h2>\n<p>As the help shows, the <strong>Remove<\/strong> method of an ArrayList removes the first instance of the item from the array list. It returns <strong>Void<\/strong>, which means nothing. The Remove method changes the original array list. It doesn\u2019t return a new array list.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image10.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image_thumb10.png\" alt=\"image\" width=\"674\" height=\"238\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<blockquote><p><span style=\"font-family: Lucida Console; font-size: small;\">PS C:\\ps-test&gt; $caps<br \/>\nA<br \/>\nB<br \/>\nD<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>To remove multiple instances of an object, use a <strong>While<\/strong> loop.<\/p>\n<pre><span style=\"color: #000000;\">    [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #8b0000;\">$caps<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #ff0000;\">\"A\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"B\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"D\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"E\"<\/span>\r\n    <span style=\"color: #0000ff;\">while<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$caps<\/span> <span style=\"color: #0000ff;\">-contains<\/span> <span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">) {\r\n        <\/span><span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.Remove(<\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">)\r\n    }<\/span><\/pre>\n<pre><\/pre>\n<p>You can also use a <strong>ForEach<\/strong> loop on an array list, but you can\u2019t remove objects from the array list that you\u2019re traversing with ForEach. In the background, ForEach uses positional indexes and the position of each object in an array list (or any collection) changes when you remove an object.<\/p>\n<p>In the following example, the code uses a ForEach loop to remove the <strong>svchost<\/strong> processes from an array list of processes.<\/p>\n<pre><span style=\"color: #000000;\">    [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #8b0000;\">$p<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"font-weight: bold; color: #0000ff;\">Get-Process<\/span>\r\n    <span style=\"color: #0000ff;\">foreach<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$process<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$p<\/span><span style=\"color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$process<\/span><span style=\"color: #000000;\">.Name <\/span><span style=\"color: #0000ff;\">-eq<\/span> <span style=\"color: #ff0000;\">\"svchost\"<\/span><span style=\"color: #000000;\">)\r\n        {\r\n            <\/span><span style=\"color: #8b0000;\">$p<\/span><span style=\"color: #000000;\">.Remove(<\/span><span style=\"color: #8b0000;\">$process<\/span><span style=\"color: #000000;\">)\r\n        }\r\n    }<\/span><\/pre>\n<blockquote><p><span style=\"color: #ff0000; font-family: Lucida Console; font-size: small;\">Collection was modified; enumeration operation may not execute.<br \/>\nAt line:1 char:10<br \/>\n+ foreach ($process in $p)<br \/>\n+ ~~~~<br \/>\n+ CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : OperationStopped: (:) [], InvalidOperationException<br \/>\n+ FullyQualifiedErrorId : System.InvalidOperationException<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Instead, create an array of <strong>svchost<\/strong> processes ($s) and use a ForEach loop to remove each svchost process from $p. Because ForEach is traversing the $s array, you can use it to remove objects from $p.<\/p>\n<pre>\u00a0<span style=\"color: #000000;\">   [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #8b0000;\">$p<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"font-weight: bold; color: #0000ff;\">Get-Process<\/span>\r\n    <span style=\"color: #8b0000;\">$s<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"font-weight: bold; color: #0000ff;\">Get-Process<\/span> <span style=\"color: #0000ff;\">|<\/span> <span style=\"font-weight: bold; color: #c00000;\">where<\/span><span style=\"color: #000000;\"> ProcessName <\/span><span style=\"color: #3399ff;\">-eq<\/span> <span style=\"color: #ff0000;\">\"svchost\"<\/span>\r\n    <span style=\"color: #0000ff;\">foreach<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$svchost<\/span> <span style=\"color: #0000ff;\">in<\/span> <span style=\"color: #8b0000;\">$s<\/span><span style=\"color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #8b0000;\">$p<\/span><span style=\"color: #000000;\">.Remove(<\/span><span style=\"color: #8b0000;\">$svchost<\/span><span style=\"color: #000000;\">)\r\n    }\r\n<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>RemoveAt: Remove Objects by Index<\/h2>\n<p>You can also use the RemoveAt method of ArrayLists to remove objects. RemoveAt takes the index of the object that you want to remove. In this example, we\u2019ll use a value of 2, which is the index of \u201cC\u201d.<\/p>\n<blockquote><p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image11.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image_thumb11.png\" alt=\"image\" width=\"558\" height=\"221\" border=\"0\" \/><\/a><\/p><\/blockquote>\n<blockquote><p><span style=\"font-family: Lucida Console; font-size: small;\">PS C:\\ps-test&gt; $caps<br \/>\nA<br \/>\nB<br \/>\nD<br \/>\nE<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>It was easy to find the index of \u201cC\u201d in such a small array list, but it you have a larger array list, you can use the <strong>IndexOf<\/strong> method to find the index and then use the <strong>RemoveAt<\/strong> method to remove the object at that index. You can call the methods in two separate commands or in a single command.<\/p>\n<p>&nbsp;<\/p>\n<pre>    <span style=\"color: #8b0000;\">$index<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.IndexOf(<\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">)\r\n    <\/span><span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.RemoveAt(<\/span><span style=\"color: #8b0000;\">$index<\/span><span style=\"color: #000000;\">)\r\n<\/span><\/pre>\n<p><span style=\"color: #000000;\">-or-<\/span><\/p>\n<pre>    <span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.RemoveAt(<\/span><span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.IndexOf(<\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">))\r\n<\/span><\/pre>\n<pre><\/pre>\n<p>To use the RemoveAt method to remove multiple objects, use a <strong>For<\/strong> loop. The For loop is well suited to this task because it use indexes.<\/p>\n<p>But, be careful! When you remove an object from a collection, you change the indexes of every subsequent object in the collection. For example, in the following array list, we want to remove the \u201cC\u201d objects at indexes 2, 3, and 4. But after we remove the \u201cC\u201d object at index 2, the index of the second \u201cC\u201d object changes from 3 to 2. If we advance the index from 2 to 3, we&#8217;ll miss the &#8220;C&#8221; that is now at index 2.<\/p>\n<blockquote><p>\n<a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image12.png\"><img loading=\"lazy\" decoding=\"async\" style=\"display: inline; border-width: 0px;\" title=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2014\/11\/image_thumb12.png\" alt=\"image\" width=\"244\" height=\"226\" border=\"0\" \/><\/a>\n<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>To compensate, whenever you remove an object, decrement the index by 1. You can use any notation to reduce it&#8217;s value by one:\u00a0 <span style=\"font-family: Lucida Console; font-size: small;\">$i = $i \u2013 1<\/span>, <span style=\"font-family: Lucida Console; font-size: small;\">$i \u2013= 1<\/span>, or <span style=\"font-family: Lucida Console; font-size: small;\">$i&#8211;<\/span>.<\/p>\n<pre><span style=\"color: #000000;\">    [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #8b0000;\">$caps<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #ff0000;\">\"A\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"B\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"D\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">, <\/span><span style=\"color: #ff0000;\">\"E\"<\/span>\r\n    <span style=\"color: #0000ff;\">for<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$i<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">; <\/span><span style=\"color: #8b0000;\">$i<\/span> <span style=\"color: #0000ff;\">-lt<\/span> <span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.count; <\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #0000ff;\">++<\/span><span style=\"color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">[<\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #000000;\">] <\/span><span style=\"color: #0000ff;\">-eq<\/span> <span style=\"color: #ff0000;\">\"C\"<\/span><span style=\"color: #000000;\">)\r\n        {\r\n            <\/span><span style=\"color: #8b0000;\">$caps<\/span><span style=\"color: #000000;\">.RemoveAt(<\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #000000;\">)\r\n            <\/span><span style=\"color: #8b0000;\">$i--<\/span><span style=\"color: #000000;\">\r\n        }\r\n    }<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Here&#8217;s the same technique used to remove the svchost processes from the $p array list.<\/p>\n<pre><span style=\"color: #000000;\">    [<\/span><span style=\"color: #0000cd;\">System.Collections.ArrayList<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #8b0000;\">$p<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"font-weight: bold; color: #0000ff;\">Get-Process<\/span>\r\n    <span style=\"color: #0000ff;\">for<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$i<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #000000;\">0<\/span><span style=\"color: #000000;\">; <\/span><span style=\"color: #8b0000;\">$i<\/span> <span style=\"color: #0000ff;\">-lt<\/span> <span style=\"color: #8b0000;\">$p<\/span><span style=\"color: #000000;\">.Count; <\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #0000ff;\">++<\/span><span style=\"color: #000000;\">)\r\n    {\r\n        <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$p<\/span><span style=\"color: #000000;\">[<\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #000000;\">].Name <\/span><span style=\"color: #0000ff;\">-eq<\/span> <span style=\"color: #ff0000;\">\"svchost\"<\/span><span style=\"color: #000000;\">)\r\n        {\r\n            <\/span><span style=\"color: #8b0000;\">$p<\/span><span style=\"color: #000000;\">.RemoveAt(<\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #000000;\">)\r\n            <\/span><span style=\"color: #8b0000;\">$i<\/span><span style=\"color: #0000ff;\">--<\/span><span style=\"color: #000000;\">\r\n        }    \r\n    }<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h2>Summary<\/h2>\n<p>For most scripts, you don\u2019t manipulate arrays directly. Instead, you use Where-Object to create a new collection that includes only the items that you need. But if you need to manage an array directly, you can add and remove objects.<\/p>\n<p><i>June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at <a href=\"mailto:juneb@sapien.com\">juneb@sapien.com<\/a> or follow her on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\">@juneb_get_help<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; A member of the PowerShell group on Facebook asked how to delete an object from an array. It&#8217;s a simple question, but the answer isn&#8217;t very simple at all. It&#8217;s one of those beginner questions that has an advanced answer. When I first answered this question in about_Arrays, I didn&#8217;t really know much about [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[932,25],"tags":[934,28],"class_list":["post-8152","post","type-post","status-publish","format-standard","hentry","category-beginners","category-windows-powershell","tag-juneb","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=8152"}],"version-history":[{"count":3,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8152\/revisions"}],"predecessor-version":[{"id":8205,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8152\/revisions\/8205"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=8152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=8152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=8152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}