Home > Software design >  How to delete a value from a variable
How to delete a value from a variable

Time:06-28

I think the question has already been asked, despite my various searches, I can't really find the answer.

I get the WSUS groups from a server and I integrate the data in a variable. I would like to delete a specific line in this variable.

For example with a part of the imaginary script :

Write-Output "Delete start"
$Var = ("one", "two", "three", "four")
$Var
$Var_end = $Var.Remove("two")

Write-Output "Delete end"
$Var_end

I want to remove the two value so that the rest of the script can retrieve what's in my variable without retrieving the two or passing on an empty line (as I've already managed to do)

I have tried several functions, in different directions but there is nothing to do I also tried with the function

$Var_end = $Var | select -skip 1

But it's not what I'm looking for because it doesn't look clean as a function, if ever the value changes, I'll have to adapt the script afterwards

CodePudding user response:

The idiomatic solution to removing items in an array is to use PowerShell's pipeline and the Where-Object cmdlet to filter the original collection and create a new one by assigning the results to a variable:

$list = 'one','two','three','four'
$filteredList = $list |Where-Object { $_ -ne 'two' }

But in this case where we're looking to exclude an exact value, you can also use the comparison operators in filter mode:

$filteredList = @($list) -ne 'two'

PowerShell will recognize that the left-hand side of the -ne operation is an enumerable collection, and will apply -ne as a filter, giving you the exact same result as if you'd used Where-Object.

Beware that neither option guarantees an array as the result - for that, you'd need to wrap the entire statement in @(...), or strongly type the receiving variable:

$filteredList = @( ... )
# or
[array]$filteredList = ...

CodePudding user response:

You already got a helpful answer that shows the normal way to solve the problem in PowerShell.

As for what you have tried:

$Var = ("one", "two", "three", "four")
$Var_end = $Var.Remove("two")

Although an Array has a method Remove(), it is non-functional, because arrays are of fixed size. So to change the size of an Array you'd have to create a new one of the desired size and copy elements from the old array into the new array. That the Remove() method even exists can be explained because the Array class implements the IList interface.

The .NET framework does provide dynamically resizable data structures that behave similar to arrays in most regards. One of these is the List class:

$var = [Collections.Generic.List[object]] ("one", "two", "three", "four")
$null = $var.Remove("two") 

The Remove() method modifies the $var object in-place. Assigning to $null avoids implicitly outputting the result of the method, which we are not interested in (it's a boolean indicating whether the item was found).

While that method works, most of the time it isn't needed and just complicates the code, because it works against PowerShell idioms. Using the filtering capabilities of PowerShell, as explained by Mathias's answer, allows us to write much cleaner code.

  • Related