1

This is my $array:

Name          GUID 
----          ---- 
PC001         AAAA 
PC001         BBBB 
PC001         CCCC 
PC002         AAAA
PC002         DDDD
PC003         AAAA
PC003         CCCC

Here's my script:

$Guid = "CCCC"
$workingName = $array | where Guid -eq $Guid | select  name
$array.remove($workingName) #broke!

What I'm trying to achieve is that if $Guid = "CCCC" it would remove all instances of the Name and CCCC from the array, where CCCC exists, so in this instance five lines (three lines for PC001, two lines for PC0003), if it was BBBB it would only remove the three lines for PC001.

FYI: this is part of a much larger script

2 Answers 2

4

Well, you could create a new variable or just update the exiting one using a where statement.

$Array | ? {$_.GUID -ne 'CCCC'}

This will return the array back without the entries

Alternatively you can use methods, like you are trying to do there but you need to build the array a bit differently...here is an example.

$Proc = Get-Process | Select -first 2
$a = New-Object System.Collections.ArrayList
$a.Add($Proc[0])
$a.Add($Proc[1])
$a
Write-Warning 'removing index 1'
$a.remove($Proc[1])
$a
Sign up to request clarification or add additional context in comments.

4 Comments

$Array | ? {$_.GUID -ne 'CCCC'} doesn't do what the Ops wants. Not only should entries with that GUID be removed but also entries that have a name, which had an entry with that GUID. Given the ops example, your code would leave PC001 AAAA, PC001 BBBB and PC003 AAAA behind however the op wants those entries removed.
I had to read his post an unfortunate amount of times before that was clear...I guess an output example would have been helpful.
NoahSparks I'm sorry I wasn't clear, and I take and accept your point about giving an output example. I'm very grateful for your efforts (I voted up), and although I was aware that your answer didn't provide the exact output I needed, it did help me develop my own answer (which was extremely close to the answer @DanL gave, I just didn't have the -notin yet). I'm also sorry I didn't have the change to update you until now, I was working on the problem at the same time in order to met a deadline.
It's alright, I am glad you were able to get it figured out.
0
$Guid = "CCCC"
$workingName = $array | where Guid -eq $Guid | select -ExpandProperty name
$array = $array | where { $_.name -notin $workingName }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.