Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey, PowerShell Guy! How can I get a list of all the subfolders in a folder and then put that list into an array?

Again a Hey Scripting Guy, re-write in PowerShell

How Can I Get a List of All the Subfolders in a Folder and Then Put That List Into an Array?

where a 30 + lines solution in VbScript is given, using WMI relation and recursion.

As arrays and objects are default in PowerShell and it`s a shell, ofcourse this was a very easy one to rewrite in powershell, and too show off the PoSH power a bit :

 

$dirs = dir -Recurse | Where {$_.psIsContainer -eq $true}

$dirs | select fullname

Take a look at the VbScript and you see that in this case the gain in time / productivity , is much more as the Factor 10 improvement that  I give as an estimate for the Productivity boost  when using PowerShell normaly, when asked  ;-)

dir standard returns an array of object (see examples) and you still have all information about the Directories as you have the complete Object in your array (also see examples).

So thats all !,

Only thing that may look a bit strange is the using of psIsContainer in the where, as PowerShell "Drives" (providers) do not have to be FileSystems this is more generic.

( As Dir is an alias, the "real" PowerShell command is : Get-ChildItem -Recurse )

this psIsContainer is a NotePropery see examples below :

Some examples :

 

PoSH> $dirs = dir -Recurse | Where {$_.psIsContainer -eq $true}                                                                             
PoSH> $dirs.GetType()                                                                                                                       
                                                                                                                                            
IsPublic IsSerial Name                                     BaseType                                                                         
-------- -------- ----                                     --------                                                                         
True     True     Object[]                                 System.Array                                                                     
                                                                                                                                            
                                                                                                                                            
PoSH> $dirs[0]                                                                                                                              
                                                                                                                                            
                                                                                                                                            
    Directory: Microsoft.PowerShell.Core\FileSystem::C:\PowerShell                                                                          
                                                                                                                                            
                                                                                                                                            
Mode                LastWriteTime     Length Name                                                                                           
----                -------------     ------ ----                                                                                           
d----        11/24/2006  11:50 AM            bar                                                                                            
                                                                                                                                            
                                                                                                                                            
PoSH> $dirs[0..3] | select name                                                                                                             
                                                                                                                                            
Name                                                                                                                                        
----                                                                                                                                        
bar                                                                                                                                         
Bits                                                                                                                                        
concentration                                                                                                                               
DemoBoston                                                                                                                                  
                                                                                                                                            
                                                                                                                                            
PoSH> $dirs[0..3] | select fullname                                                                                                         
                                                                                                                                            
FullName                                                                                                                                    
--------                                                                                                                                    
C:\PowerShell\bar                                                                                                                           
C:\PowerShell\Bits                                                                                                                          
C:\PowerShell\concentration                                                                                                                 
C:\PowerShell\DemoBoston                                                                                                                    
                                                                                                                                            
                                                                                                                                            
PoSH> $dirs | Get-Member -MemberType property                                                                                               
                                                                                                                                            
                                                                                                                                            
   TypeName: System.IO.DirectoryInfo                                                                                                        
                                                                                                                                            
Name              MemberType Definition                                                                                                     
----              ---------- ----------                                                                                                     
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}                                                                 
CreationTime      Property   System.DateTime CreationTime {get;set;}                                                                        
CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;}                                                                     
Exists            Property   System.Boolean Exists {get;}                                                                                   
Extension         Property   System.String Extension {get;}                                                                                 
FullName          Property   System.String FullName {get;}                                                                                  
LastAccessTime    Property   System.DateTime LastAccessTime {get;set;}                                                                      
LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;}                                                                   
LastWriteTime     Property   System.DateTime LastWriteTime {get;set;}                                                                       
LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;}                                                                    
Name              Property   System.String Name {get;}                                                                                      
Parent            Property   System.IO.DirectoryInfo Parent {get;}                                                                          
Root              Property   System.IO.DirectoryInfo Root {get;}                                                                            
                                                                                                                                            
                                                                                                                                            
PoSH> $dirs | Get-Member -MemberType noteProperty                                                                                           
                                                                                                                                            
                                                                                                                                            
   TypeName: System.IO.DirectoryInfo                                                                                                        
                                                                                                                                            
Name          MemberType   Definition                                                                                                       
----          ----------   ----------                                                                                                       
PSChildName   NoteProperty System.String PSChildName=bar                                                                                    
PSDrive       NoteProperty System.Management.Automation.PSDriveInfo PSDrive=C                                                               
PSIsContainer NoteProperty System.Boolean PSIsContainer=True                                                                                
PSParentPath  NoteProperty System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\PowerShell                                   
PSPath        NoteProperty System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\PowerShell\bar                                     
PSProvider    NoteProperty System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem                        
                                                                                                                                            
                                                                                                                                            
PoSH>                                                                                                        

 

But also the "advanced" methods used in the VbScript solution can be implemented easy in PowerShell, see the following posts on my old blog :

Using WMI to get Subdirectories : 

PowerShell : Getting Subdirectories using WMI

Also you can find a nice example of using recursion ( the DoClear() function)  and using windows forms in this MineSweeper Game example on my old blog here  :

/\/\o\/\/ PowerShelled: MSH Minesweeper GUI game

 

Thanks to the Scripting Guy, for giving this great Pass  again, to Show how amazing PowerShell is with a rewrite of his VbScript version  ;-)

Enjoy,

Greetings /\/\o\/\/

Published Saturday, January 27, 2007 12:59 PM by admin
Filed under:

Comments

# re: Hey, PowerShell Guy! How can I get a list of all the subfolders in a folder and then put that list into an array?

Hi MOW,

I have written a post a few weeks ago about the recurse and how to discover if a item returned by get-childitem is a directory ir not. (but in Portuguese).

I have made some tests and I discovered that use PSIsContainer is about 33% faster than use -band operator on the attributes property. But Powershell uses the second solution for each dir we type... :

(Get-Item teste.txt) | gm | where {$_.name -eq 'Mode'} | fl definition

I wrote to Jeffrey with some suggestions, like an extra parameter to Get-ChildItem, to retrieve only Containers (directories, in this case)... so, lets wait v1.1 of Powershell...

My blog, in Portuguese:

http://viniciuscanto.blogspot.com/2007/01/dir-ad-no-powershell-onde-ele-foi-parar.html">http://viniciuscanto.blogspot.com/2007/01/dir-ad-no-powershell-onde-ele-foi-parar.html

Thank you and congratulations for your blog... it's very good.

--

Vinicius Canto <scripterbr_at_gmail_dot_com>

MVP Visual Developer - Scripting

MCP Windows 2000 Server, Windows XP e SQL Server 2000

Blog sobre Scripting em português: http://viniciuscanto.blogspot.com

Saturday, January 27, 2007 4:22 PM by Vinicius Canto [MVP]

# re: Hey, PowerShell Guy! How can I get a list of all the subfolders in a folder and then put that list into an array?

hiya Vinicius,

thanks for the comment, I follow your blog also

and agree on your comments, also that get-ChildItem not alway works as we might expect / want a bot topic on the NG, only could not resist this post as the VbScript solution was that big ;-)

Greetings /\/\o\/\/

Saturday, January 27, 2007 6:30 PM by MoW

# Adminspotting.net &raquo; PowerShell: TMTOWTDI

Tuesday, January 30, 2007 10:23 AM by Adminspotting.net » PowerShell: TMTOWTDI

# Scripting Games MMVII PowerShell Competition

As I metioned before on my blog and you can see on the Scripting Games Home the Scripting Games MMVII

Monday, February 05, 2007 4:14 PM by The PowerShell Guy
Anonymous comments are disabled