# /\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")$PreviousItems = (" PowerShell : Getting Subdirectories using WMI "," PowerShell basic parameter checking of a function "," PowerShell Celsius Fahrenheit converting "," PowerShell : How Can I Rename Files Using File Nam... "," PowerShell make a drive of an UNC path "," Some Powershell News "," PowerShell Import and Export a DirectoryTree and S... "," PowerShell How Can I Query a Text File and Retriev... "," PowerShell Import Shares and Security info From CS... "," An other PowerShell Blog is born "," ")

## PowerShell : Getting Subdirectories using WMI part 2

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 : Comments: applepwc hello,mow: A little mistake: Function get-DirTree ($name) {
should be
Function get-SubDirs ($name) { ########################## I changed your script a little: Function get-SubDirs ($name) {
$name=$name.replace('\','\\')
(gwmi Win32_directory -filter "name = '$name'").getrelated('Win32_directory') | where {$_.name -match $name} |% {$_.name
get-SubDirs $_.name } } /\/\o\/\/ hi Applewc, Oops, I did change my mind about the name while testing, but gorgot to edit. gr /\/\o\/\/ Pete Gomersall MOW, See "more on wmi folder security" in the NG. Pete Pete Gomersall MOW, So this type of directory check bombs out when there are special characters in the folder name e.g. "Pete's Folder". I looked at the post on your site related to System.Management.RelatedObjectQuery but was not sure how to implement it in this case. Pete /\/\o\/\/ Pete : (gwmi Win32_directory -filter "name = 'c:\\foobar'").getrelated($null,"Win32_SubDirectory",$null,$null,$null,"GroupComponent",$false,$null) gr /\/\o\/\/ Pete Gomersall MOW: So using the following: Function get-SubFolder ($name) {
$name=$name.replace('\','\\')
(gwmi Win32_directory -filter "name = '$name'").getrelated($null,"Win32_SubDirectory",$null,$null,$null,"GroupComponent",$false,$null) |% {$_.name