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\/\/