Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to use get-childitem recursively, but only have it return files not directories. The best solution I have just doesn't seem natural:

gci . *.* -rec | where { $_.GetType().Name -eq "FileInfo" }
share|improve this question
up vote 44 down vote accepted

Try this:

gci . *.* -rec | where { ! $_.PSIsContainer }
share|improve this answer

In Powershell 3.0, it is simpler,

gci -Directory #List only directories
gci -File #List only files

This is even shorter,

gci -ad # alias for -Directory
gci -af # alias for -File
share|improve this answer

In PowerShell 3.0 you can also use the newly added -Attributes parameter
(together with logical operators)

Get-ChildItem -Recurse -Attributes !Directory+!System


dir -r -Attributes !D
share|improve this answer

In powershell 2.0 the best and simplest solution i came up with is to include all files with an extension:

get-childitem -Recurse -include *.*

folders doesn't have an extension so they are excluded, beware of no extension named files.

share|improve this answer
This (about directories) is not true. Nothing can stop you to have directory with extension – Romeo Ninov May 7 '15 at 11:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .