# get-childitem directories only

**URL:** <https://forums.powershell.org/t/get-childitem-directories-only/5143>\
**Category:** PowerShell Help\
**Created:** [November 1, 2015, 2:52am UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143 "2015-11-01T02:52:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shane-lindberg](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@shane-lindberg](https://forums.powershell.org/u/shane-lindberg)\
**Post date:** [November 1, 2015, 2:52am UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143/1 "2015-11-01T02:52:39Z")

</div>

I am trying to do the following in powershell version 2

I have been trying to figure out how to filer only directories. I came to this solution

```
get-childitem | where {$_.getdirectories}
```

This is when the confusion started. When I do a get-member of get-childitem I get two main categories: system.io.directoryinfo and system.io.filename.

question 1:  
when I do the following command what category is it using: directoryinfo or filename? they share some of the same names of attributes and methods

```
get-childitem | where {$_.somethinghere}
```

question 2:

with the following command I and using $_.getdirectories and not $_.getdirectories(). I thought all methods needed the “()”. When I do that it fails

```
get-childitem | where {$_.getdirectories}
```

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [November 1, 2015, 3:10am UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143/2 "2015-11-01T03:10:52Z")

</div>

First problem is the GetDirectories is a method not a property - you can’t use the way you were trying. The easiest way to get just the directories in PS 2

Get-ChildItem | where {$\_.PSIsContainer}

if you want just files use  
Get-ChildItem | where {-not $_.PSIsContainer}  
or  
Get-ChildItem | where {!$_.PSIsContainer}

To use GetDirectories() try  
$d = Get-Item -Path $pwd  
$d.GetDirectories()

if you just want files try  
$d.GetFiles()

---

<div class="post-metadata">

**Author:** ![shane-lindberg](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@shane-lindberg](https://forums.powershell.org/u/shane-lindberg)\
**Post date:** [November 1, 2015, 3:15am UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143/3 "2015-11-01T03:15:20Z")

</div>

That was part of my confusion, when I used it as a property(it was a mistake, but it worked) it did as I wanted. I will look at your examples to do it properly. Any idea to why it worked when I used it as a property? Just curious.

---

<div class="post-metadata">

**Author:** ![tim-curwick](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-curwick/32/431_2.png) [@tim-curwick](https://forums.powershell.org/u/tim-curwick)\
**Post date:** [November 2, 2015, 2:00am UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143/4 "2015-11-02T02:00:09Z")

</div>

Shane,

When you use a method name without the parentheses, PowerShell returns information about the method. And when any non-empty thing (like information about a method) is converted to a boolean, it evaluates as $True.

FileInfo objects, on the other hand, not having either a method nor a property named GetDirectories, return a $Null. When converted to boolean, $Null is evaluated as $False.

So effectively you were testing for the existence of either a method with that name or a property with that name with a non-empty value. Mistake or not, it’s kind of a neat trick.

---

<div class="post-metadata">

**Author:** ![shane-lindberg](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@shane-lindberg](https://forums.powershell.org/u/shane-lindberg)\
**Post date:** [November 2, 2015, 4:59am UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143/5 "2015-11-02T04:59:48Z")

</div>

Cool. Thanks for explaining that.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:44pm UTC](https://forums.powershell.org/t/get-childitem-directories-only/5143/6 "2024-05-16T20:44:33Z")

</div>


