Getting folder tree a few levels deep

I don’t know about you guys, but there are times you need to figure out security Inheritance on a folder tree. You generally don’t have to go through the entire tree (if you’re lucky) just a few subfolders deep. When setting NTFS rights on a folder, rule of thumb is to go three levels deep (Ok maybe four) so that’s a good place to start. To get a general impression of a folder tree, you’d basically go three levels deep. Enumerating subfolders in levels, helps in circumventing the dreaded Max_256 error, although, if you run into errors at level 3, then brace yourself…

The first step is getting a list of all the subfolders you want to enumerate. Here’s my take on how get that list 😉

The trick to getting the subfolder is adding “\*” to the folder you want to enumerate.

“\*” will get me all of the first level subfolders

“\*\*” will get me all of the second level subfolders etc. etc.

The script is pretty straightforward.


<#

Author: ing. I.C.A. Strachan
Version: 1.0.0
Version History: 

Purpose: Get Subfolders from a Folder up to 6 levels deep
         whichever comes first. Output is presented in Out-GridView

Syntax: Get-FolderListLevel -FolderPath <Folder> -Level <Default is 3 >

#>

[CmdletBinding()]
param(
    [string]$FolderPath = $env:USERPROFILE ,

    [ValidateRange(1,6)]
    [int]$level=3
)

$arrExportFolders = @()

for ($i=0;$i -le $level;$i++) {
    $subLevel ='\*' * $i

    if ( $i -eq 0){
        $arrExportFolders += Get-Item "$($FolderPath)$($subLevel)" |
        Select-Object FullName,
        @{Name='Level'; Expression={$i}},
        @{Name='FolderName'; Expression={$_.name.PadLeft($_.name.length + ($i* 5),'_')}}
    }
    Else {
        $arrExportFolders += Get-ChildItem "$($FolderPath)$($subLevel)" -Directory|
        Select-Object FullName,
        @{Name='Level'; Expression={$i}},
        @{Name='FolderName'; Expression={$_.name.PadLeft($_.name.length + ($i* 5),'_')}}
    }
}

$arrExportFolders |
select-object FolderName,Level,FullName |
Sort-Object FullName |
Out-GridView -Title "Folder list $($FolderPath) - $($level) Levels deep - $(get-date)"

Line 37 is a little gem I got from Ashley McGlone’s  gPLink_report script It will pad the folder’s name attribute on the left side with 5x ‘_’ the current level.

This will give you an impression of the folder tree much like tree command 🙂

I’m sure you’ll find other uses once you have your list…

Hope it’s worth something to you!

Ttyl,

Urv

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s