Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

4 Answers 4

up vote 35 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

Golfed

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
1  
This (about directories) is not true. Nothing can stop you to have directory with extension –  Romeo Ninov May 7 at 11:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.