Displaying Folder Tree

PowerShell is a friend with old console commands, so the easiest way of displaying the tree structure of a folder is the old “tree” command. It works best in a native PowerShell console because editors often use a different character set. Try this:

 
PS> Tree $home 
 

Just make sure you are running this command in a native PowerShell console or VSCode. You can then pipe the result to clip.exe and paste it in documentation documents:

 
PS> Tree $home | clip.exe 
 

Twitter This Tip! ReTweet this Tip!

  • # Let's automate this a bit more

    Tree $home | clip.exe

    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

    & "$env:WinDir\Notepad.exe"

    $a = Get-Process | Where-Object {$_.Name -eq "Notepad"}

    [Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)

    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")

    [System.Windows.Forms.SendKeys]::SendWait("%EP")

  • # or as a function to add to your profile

    function Get-FolderTree ($TargetFolder = $home)

    {

       Tree $TargetFolder | clip.exe

       [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

       & "$env:WinDir\Notepad.exe"

       $a = Get-Process | Where-Object {$_.Name -eq "Notepad"}

       [Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)

       [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")

       [System.Windows.Forms.SendKeys]::SendWait("%EP")

    }

    # Use the default user home folder

    Get-FolderTree

    # Use another folder.

    Get-FolderTree -TargetFolder 'D:\Temp'

  • This looks great in the PS console, but after pasting I get accented characters instead of the lines.  What am I missing?