Geeks With Blogs
Technically Speaking .NET Bloggers Sharing Ideas

It seems so obvious that getting a directory listing excluding file using the Get-ChildItem commandlet should be simple, right? Well, it is simple, but not obvious from the built-in commandlet commands. Here’s one way you can accomplish this task:

# initialize the items variable with the

# contents of a directory

$items = Get-ChildItem -Path "c:\temp"

 

# enumerate the items array

foreach ($item in $items)

{

      # if the item is a directory, then process it.

      if ($item.Attributes -eq "Directory")

      {

            Write-Host $item.Name

      }

}

You can also return files and exclude directories with this simple change:

# initialize the items variable with the

# contents of a directory

$items = Get-ChildItem -Path "c:\temp"

 

# enumerate the items array

foreach ($item in $items)

{

      # if the item is NOT a directory, then process it.

      if ($item.Attributes -ne "Directory")

      {

            Write-Host $item.Name

      }

}

 

Technically Speaking author, Ethan Wilansky

Posted on Thursday, November 6, 2008 4:48 PM Powershell | Back to top


Comments on this post: Getting a Directory or File Listing From PowerShell

# re: Getting a Directory or File Listing From PowerShell
Requesting Gravatar...
Hi Ethan,

Thanks posting this. While we don't give you this functionalty out of the box, we do proved a little bit of help. PowerShell adds an extra property to all objects emitted from Get-ChildItem named PSIsContainer. This property will be set for all providers (registry, cert store, etc.) not just the file system so it's more portable. This simplifies things a bit so you can just write

foreach ($i in Get-ChildItem c:\temp) { if ($i.PSisContainer) {$i }}

or, using the pipeline and the where-object cmdlet,

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

-bruce

Bruce Payette,
Microsot Corporation
Left by Bruce Payette on Nov 06, 2008 10:12 PM

# re: Getting a Directory or File Listing From PowerShell
Requesting Gravatar...
Thanks for this excellent advice! You're right that this is much more flexible.
In order to display just the name, I augmented your example using the pipeline to:

Get-ChildItem c:\temp | where {$_.PSIsContainer} | write-host

and for the script example, I specified the Name property:
foreach ($i in Get-ChildItem c:\temp) { if ($i.PSisContainer) {$i.Name }}

The goal here was to provide the same output as my original post.

Thanks again for this really important advice,
Ethan Wilansky
Left by Ethan Wilansky on Nov 07, 2008 5:31 PM

# re: Getting a Directory or File Listing From PowerShell
Requesting Gravatar...
The code works nicely but I happened to notice that it excluded some directories. If a directory had only the Directory attribute, it worked fine but if it had "Directory, Archive" the code would not list it.

I tried a different approach where I'm searching in attributes for Directory as below:

$dirattrib = [string] $item.Attributes

# if the item is a directory, then process it.
if ($dirattrib.Contains("Directory"))
{
Write-Host $item.Name
Left by Chris Leblanc on Nov 11, 2010 8:03 AM

# re: Getting a Directory or File Listing From PowerShell
Requesting Gravatar...
Getting directory listings is very handy for good workflow on some work I'm doing right now. With this post I could make a script to do just what's needed. I will share that ..

# Given a directory path and an output file name, this script will fetch all file names from the
# given directory and append them in the output file.

$dir = Read-Host 'Enter the directory full path.'
$outfile = Read-Host 'Enter the output file full path.'

$items = Get-ChildItem $dir

foreach ($i in $items){
if ($i.Attributes -ne 'Directory'){
$i.Name >> $outfile
}
}
Left by Randy on Sep 02, 2012 12:19 PM

# re: Getting a Directory or File Listing From PowerShell
Requesting Gravatar...
you can achieve this using the single statement as below


#Gives the list of file i.e exclude directory
Get-ChildItem -Path "c:\temp" | where-object {$_.Attributes -ne "Directory"}


#Gives the list of Directory exclude file
Get-ChildItem -Path "c:\temp" | where-object {$_.Attributes -eq "Directory"}
Left by Milind Khadloya on Nov 08, 2012 5:38 PM

Your comment:
 (will show your gravatar)


Copyright © TechnicallySpeaking | Powered by: GeeksWithBlogs.net