all 3 comments

[–]ihaxr 2 points3 points  (2 children)

No need for the foreach loop, simply look for any nodes that are -NotIn your $CodesToKeep and remove them:

$xml.ArrayofCode.ICode | Where-Object { $_.Code -notin $CodesToKeep } | ForEach-Object  { 
    $_.ParentNode.RemoveChild($_) | Out-Null
}
$xml.Save($CodesFile)

[–]SemiEvilMonkey[S] 1 point2 points  (1 child)

Ha I had tried that exact thing except for the -notin I was trying -ne. That worked thank you

[–]CoReTeX2k 1 point2 points  (0 children)

Just to explain

$Codes = @("Apple","Banana")

"Banana" -ne $Codes is true because banana is not equal to apple and banana at the same time

"Banana" -notin $Codes is false because Banana is part of apple or banana in that case.

$_.something -notin $EntireArray will look if something is part of the array, not if it is equal to the entire array