2

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.

1 Answer 1

4

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}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, definitely an example, I left out the 10 lines that compare DB results to folder structure. I swear I tried ExpandProperty... thanks.
@Kavius We all make silly mistakes. Best way to learn! I know.
I updated the example to take into account some PSv2 differences when I moved the script to the deployment machine.

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.