Reputation: 2889
I am attempting to delete some orphaned folders. I have a list of folder names that are orphaned, and I am attempting to find the corresponding File object. Unfortuantely, I can't seem to get the -in
clause to work as I would expect.
A slightly contrived example:
$orphans = ls | select Name
ls | ?{$_.Name -in $orphans}
ls -Include $orphans
No files are returned.
Upvotes: 2
Views: 3063
Reputation: 46710
In order for this to work you need to do
$orphans = ls | select -ExpandProperty Name
Or inside the Where-Clause
ls | ?{$_.Name -in $orphans.name}
$orphans
in your code is an object array with a name property. Break that property out into a simple string array and you should get the results you expect.
I will also assume that the code sample is just that... a sample. That code, once working correctly, seems redundant.
Powershell v2
The above code didn't work on Powershell v2. A combination of -ExpandProperty
and -contains
seemed to correct the issue:
$orphans = ls | select -ExpandProperty Name
ls | ?{$orphans -contains $_.Name}
Upvotes: 4