Skip to content

Instantly share code, notes, and snippets.

@kvprasoon
Last active March 15, 2019 19:28
Show Gist options
  • Save kvprasoon/4d5d9b8ff2a27d0fd185c57cc5538036 to your computer and use it in GitHub Desktop.
Save kvprasoon/4d5d9b8ff2a27d0fd185c57cc5538036 to your computer and use it in GitHub Desktop.
Cross platform Show-Tree.ps1
<#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
#>
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)
$PSDriveName = $d.PSDrive.Name
if($IsWindows){
$PSDriveName = ($d.PSDrive.Name + ":")
}
$PSDriveFullPath = Join-Path $PSDriveName $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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment