Option 1 - Set the item to $null. This does not actually remove the item, but for most purposes it serves well. $servers[3] = $null
$null seems to work on display, but outputing text file has all the lines set to null back again.
Option 2 - Create a new collection which is a subset of the first. Drawback here is double the memory as the collection is copied in place. $servers = $servers -ne “itemthatyouwantremoved”
Problem here is that I want multiple blank lines changed to one blank line and then remove all blank lines from end of file.
Option 3 - Use system.collection.arraylist instead of a generic array. More steps, but the item or items are removed, and it much more efficient than option 2. The Scripting Guys explain it well in one of their PowerShell tips of the week. $servers = new-object system.collection.arraylist; $servers.Remove(”item”)
Created the arraylist, but then get-content converted it back to system.object which did not have a remove method. removerange would be preferred as I need to distinguish by place in file.
As a side issue, how would I read a line and then write the line to a different array? Will $NewArray = $_ or $NewArray.add($_) work in conjunction of test for current line is blank, last line was blank, skip, else copy line.