Show-Tree.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 3b5ee706-249d-4108-8126-c8692d603ebc
 
.AUTHOR Jeffrey Snover
 
.COMPANYNAME Microsoft Corporation
 
.COPYRIGHT (C) Microsoft Corporation. All rights reserved.
 
.TAGS Nano
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
.Synopsis
   Show an ASCII text representation of a namespace tree
.DESCRIPTION
 Script to show the layout of PowerShell namespaces (Trees) using ASCII
.EXAMPLE
PS> show-tree C:\ -MaxDepth 2
 
.EXAMPLE
PS> show-tree C:\ -MaxDepth 2 -NotLike Wind*,P* -Verbose
 
.EXAMPLE
PS> New-PSDrive -PSProvider Registry -Root HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\ -Name WINDOWS
PS> Show-Tree Windows: -maxdepth 1 -ShowDirectory
#>

[CmdletBinding()]
[Alias("tree")]
[OutputType([string[]])]
Param
(
    [Parameter(Position=0)]
    $Path = (pwd),

    [Parameter()]
    [int]$MaxDepth=[int]::MaxValue,

    #Show the full name of the directory at each sublevel
    [Parameter()]
    [Switch]$ShowDirectory, 

    #List of wildcard matches. If a directoryname matches one of these, it will be skipped.
    [Parameter()]
    [String[]]$NotLike = $null,
 
    #List of wildcard matches. If a directoryname matches one of these, it will be shown.
    [Parameter()]
    [String[]]$Like = $null
 
)
function Show-Tree
{
    [CmdletBinding()]
    [Alias("tree")]
    [OutputType([string[]])]
    Param
    (
        [Parameter(Position=0)]
        $Path = (pwd),

        [Parameter()]
        [int]$MaxDepth=[int]::MaxValue,

        #Show the full name of the directory at each sublevel
        [Parameter()]
        [Switch]$ShowDirectory, 

        #List of wildcard matches. If a directoryname matches one of these, it will be skipped.
        [Parameter()]
        [String[]]$NotLike = $null,
 
        #List of wildcard matches. If a directoryname matches one of these, it will be shown.
        [Parameter()]
        [String[]]$Like = $null,
 
        #Internal parameter used in recursion for formating purposes
        [Parameter()]
        [int]$_Depth=0
    )

    if ($_Depth -ge $MaxDepth)
    {
        return
    }
    $FirstDirectoryShown = $False
    $start = "| " * $_Depth
    :NextDirectory foreach ($d in Get-ChildItem $path -ErrorAction Ignore |where PSIsContainer -eq $true)
    {
        foreach ($pattern in $NotLike)
        {
            if ($d.PSChildName -like $pattern)
            {
                Write-Verbose "Skipping $($d.PSChildName). Not like $Pattern"
                continue NextDirectory;
            }
        }
        $ShowThisDirectory = $false
        if (!$like)
        {
            $ShowThisDirectory = $true
        }else
        {
            foreach ($pattern in $Like)
            {
                if ($d.PSChildName -like $pattern)
                {
                    Write-Verbose "Including $($d.PSChildName). Like $Pattern"
                    $ShowThisDirectory = $true
                    Break;
                }
            }            
        }
        # When we dir a Get-ChildItem, we get the OS view of the object so here we need to transform
        # it into the PowerShell view of the path (e.g. to deal with PSDrives with deep ROOT directories)
        $ProviderPath     = $ExecutionContext.SessionState.Path.GetResolvedProviderPathFromPSPath($d.PSPath, [Ref]$d.PSProvider)
        $RootRelativePath = $ProviderPath.SubString($d.PSDrive.Root.Length)
        $PSDriveFullPath  = Join-Path ($d.PSDrive.Name + ":") $RootRelativePath

        if ($ShowThisDirectory)
        {
            if (($FirstDirectoryShown -eq $FALSE) -and $ShowDirectory)
            {
                $FirstDirectoryShown = $True
                Write-Output ("{0}{1}" -f $start,$Path)
            }
            Write-Output ("{0}+---{1}" -f $start, (Split-Path $PSDriveFullPath -Leaf))
        }
        show-Tree -path:$PSDriveFullPath -_Depth:($_Depth + 1) -ShowDirectory:$ShowDirectory -MaxDepth:$MaxDepth -NotLike:$NotLike -Like:$Like
    }    
}
show-tree @PSBOundParameters
<#
 
cls
#show-tree exchange:\ -Maxdepth 3 -ShowDirectory
#show-tree C:\ -ShowDirectory -Like *ea*
#show-tree C:\windows -Maxdepth 3 -ShowDirectory -NotLike temp,amd*
cls;show-tree C:\windows -Maxdepth 6 -ShowDirectory -NotLike temp,amd*,wow*,msil*,x86*,Microsoft*,w*,s*
#cls;show-tree HKLM:\SOFTWARE\c* -ShowDirectory -NotLike classes*,cli*
#>

#EOF