1

I have two different objects in Powershell:

  • One is using a "Project" class instantiated with New-Object. Each of these contain 2 properties.

  • The second one is just an array of strings.

Now, I would like to remove the projects in the array of Project that are not in the second array.

For, instance. In Linq, I would do something like this:

var result = from item in arrayOfStrings
             from project in arrayOfProjects
             where project.Property2 == item
             select project;

This query would get me anything in the first list that is not in the second one.

What I have tried without success:

$result = $projects | Where-Object { $_.Property2 -eq $arrayOfStrings | Select-Object }

Thank you

Edit

The class looks like this:

public class Project
{
    public string Name {get; set;}
    public string Guid {get; set;}
}

The second array is just filled with some Guids. I want to get a $queryResult with the Projects that are in the second array.

3
  • Can you not edit the question and show the structure of the objects? Hard to know what you're asking otherwise. However, I don't think you'll need the final Select-Object, there Where-Object will do the "select" Commented Jul 23, 2015 at 14:55
  • I added the class in the edit. Yes, I guess I won't need it. But, I am not too familiar with Powershell Commented Jul 23, 2015 at 14:58
  • 1
    $projects | Where-Object Property2 -in $arrayOfStrings Commented Jul 23, 2015 at 15:06

1 Answer 1

5
$projects | Where-Object { $arrayOfStrings -Contains $_.Name }

Or using the "in" operator:

$projects | Where-Object { $_.Name -in $arrayOfStrings }
Sign up to request clarification or add additional context in comments.

2 Comments

Note that -in started in PowerShell version 3.
Thanks so much to both of you

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.