Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:55:23+00:00 2026-05-15T14:55:23+00:00

Imagine I have a directory structure like so: parentDir\dirA\foo\ parentDir\dirB\foo\ parentDir\dirC\ parentDir\dirD\bar\foo\ parentDir\dirE\foo\ parentDir\dirF\

  • 0

Imagine I have a directory structure like so:

parentDir\dirA\foo\
parentDir\dirB\foo\
parentDir\dirC\
parentDir\dirD\bar\foo\
parentDir\dirE\foo\
parentDir\dirF\

I want to select only the directories that a) are immediate children of parentDir, and b) have a foo directory as an immediate child.

So dirs A, B, and E qualify, whilst C, D, and F don’t.

I first tried this powershell script:

$rootDir = gi(".")
dir -include "*foo*" -recurse | ? {$_.PSIsContainer -eq $true} | % {$_.Parent} 
    | ? { $_.Parent -eq $rootDir } 

It finds all foo items below the root dir, filters out non-directories, goes up one to the intermediate directories, then filters out any directory which is not a child of the root dir.

This returns an empty list.

However if I change the last where clause as follows:

? { $_.Parent.FullName -eq $rootDir.Fullname }

It works, and I get the correct directories output.

So my question is, why won’t a direct compare between directory objects work? It may seem like a little thing but it implies there’s some hole in my understanding of powershell objects.

(Also, feel free to critique my powershell coding style in the scripts, or point out quicker/more correct ways of doing this)

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:55:24+00:00Added an answer on May 15, 2026 at 2:55 pm

    When comparing .NET objects, there are a number of factors at play. First, the compare can be a simple reference equality comparison i.e. does this variable reference the exact same object that the other variable references. In many cases (most reference types), this is the default behavior for equality. There are other types (value types) however that do equality based the value of the fields in an object e.g. DateTime, TimeSpan, Int32, Double, etc. Then there are objects override the default behavior by overriding operator == or and/or overriding Equals() virtual method. String is a reference type that overrides these to provide “value-based” equality.

    See if this does the trick:

    $rootDir = gi .
    gci . -r *Foo* | ?{$_.PSIsContainer -and $_.Name -eq 'Tools' -and `
                       $_.Parent.Name -eq $rootDir.Name}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have two directories, foo and bar in a parent directory, and these
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine I have a cell that I want to be red if the value
Imagine you have 2 databases : xpto & zpto. I want to do a
Imagine situation: I have view directory with tons of different views. all views have
I have a tree like structure created out of models using ForeignKey linkages. As
Imagine I have a nested array structure. var nested = [ [1], [2], [3]
I have an initializer in my config directory, which is something like this: ActiveSupport::Notifications.subscribe
Imagine I have a 'base' path object, denoting a directory, and a 'relative' path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.