Hairy Dresden
Hairy Dresden

Reputation: 160

How can I compare an object to an array with Powershell?

I am working with two data sets. One is used to filter the other. The base dataset is a set of folders using a naming scheme like this:

003
003 rev 1
003 rev 2
004
005
etc...

I have a second set of numbers that signify which folders I want to pull from the first set. I import them using

$numSet = Import-Csv $csvLoc | Where {$_.ItemCategory -eq "CatagoryName"} | Select-Object -ExpandProperty "Folder ID"

and they looks something like this:

C:\>$numSet
003
005

So far this is exactly how I want it. Now what I want to do is, using the Get-ChildItem command, import the locations of the folders so that I can work with them.

# This is about how I believe I should go about this.
$folderLocationArray = Get-ChildItem -Path $sourceDir | Where {$numSet.Contains($_.Name)}

Because I need to check against an array the obvious choice would be -contains, except that doesn't allow me to use wildcards, and the rev # that comes after some of the file names means that those files don't show up. This is what happens:

C:\>$folderLocationArray
003
005

I have also tried -match and -like with the same results. Instead of an error, the script either sits and acts like it is running, or returns a blank like this:

C:\>script.ps1 #This is set up to output $folderLocationArray

C:>

I know that the basics are sound, because I can plug in a single number and it works, and even with an array as the input it still returns files, just not the ones with rev # in them.

I'm at a loss. How can I use Get-ChildItem and filter the array against an array?

Edit: To clarify.

I have have a list of numbers. I have a bunch of folders. Every folder starts with a number. Some share the same number. I want to know which folders match the list.

Upvotes: 1

Views: 136

Answers (2)

lit
lit

Reputation: 16236

This will use a filter based on an item in the $numset array

$numset = @('003', '005')

foreach ($base in $numset) {
    Get-ChildItem -Directory -Recurse -Filter $($base + "*")
}

Upvotes: 3

ArcSet
ArcSet

Reputation: 6860

Filter Results based on array

@("001","002","023","006") | foreach{Get-ChildItem -Path $path -filter "$_*" -Recurse -Directory}

Upvotes: 3

Related Questions