This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
In the last post
PowerShell : Getting Subdirectories using WMI ,
I did use the WMI relation to get a folders subfolder.
Pete did ask 3 questions in the comments of the last post.
I think they were interesing enough to make a new post,
first about using $name and just the string of the directoryname,
as I made commandline examples I did just type in the string, and did $name = 'c:\foo' as you would do that in the Share example (it would be the path of the share there, i give that example in the NG I think.)
I just mixed it up in the examples by acident sorry if that was confusing.
but for the second question how to handle the escapingm if you get back a path without the escaping you can use the replace method.
$_.name.replace('\','\\')
you can see how I used it in the recursive sample below
and the last question, about recursion,
we can make a recursive function in PowerShell to get all the subdirectories,
in the sample below the function does just call itself again for each subdirectory found. so that it wil recurse the complete subtree.
# recursive directory iterating using wmi
Function get-DirTree ($name) {
(gwmi Win32_directory -filter "name = '$name'").getrelated('Win32_directory') |
where {$_.name -match $name} |% {
$_.name
get-SubDirs $_.name.replace('\','\\')
}
}
# Example
MowPS>get-SubDirs c:\\foobar
c:\foobar\bar
c:\foobar\foo
c:\foobar\foo\bar
Greetings /\/\o\/\/
Tags : Monad msh PowerShell