43

I'm writing a PowerShell script to make several directories and copy a bunch of files together to "compile" some technical documentation. I'd like to generate a manifest of the files and directories as part of the readme file, and I'd like PowerShell to do this, since I'm already working in PowerShell to do the "compiling".

I've done some searching already, and it seems that I need to use the cmdlet "Get-ChildItem", but it's giving me too much data, and I'm not clear on how to format and prune out what I don't want to get my desired results.

I would like an output similar to this:

Directory
     file
     file
     file
Directory
     file
     file
     file
     Subdirectory
          file
          file
          file

or maybe something like this:

+---FinGen
|   \---doc
+---testVBFilter
|   \---html
\---winzip

In other words, some kind of basic visual ASCII representation of the tree structure with the directory and file names and nothing else. I have seen programs that do this, but I am not sure if PowerShell can do this.

Can PowerShell do this? If so, would Get-ChildItem be the right cmdlet?

7
  • 2
    Like tree under CMD? You could get the info with Get-ChildItem and organise the output to the host depending on the items returned yes. Commented Dec 12, 2014 at 15:42
  • 1
    Oooooo, tree gives me the directory structure. That would be great! Is there a way to get the files in there too, or something similar to that? Commented Dec 12, 2014 at 15:48
  • 2
    Yes Tree /F will do it. Commented Dec 12, 2014 at 15:59
  • 1
    There is also Show-Tree from the PSCX Commented Dec 12, 2014 at 16:06
  • possible duplicate of How to save file structure to text file? Commented Dec 12, 2014 at 16:07

6 Answers 6

62

In your particular case what you want is Tree /f. You have a comment asking how to strip out the part at the front talking about the volume, serial number, and drive letter. That is possible filtering the output before you send it to file.

$Path = "C:\temp"
Tree $Path /F | Select-Object -Skip 2 | Set-Content C:\temp\output.tkt

Tree's output in the above example is a System.Array which we can manipulate. Select-Object -Skip 2 will remove the first 2 lines containing that data. Also, If Keith Hill was around he would also recommend the PowerShell Community Extensions(PSCX) that contain the cmdlet Show-Tree. Download from here if you are curious. Lots of powerful stuff there.

Sign up to request clarification or add additional context in comments.

3 Comments

Tree doesn't seem to show hidden files/folders, and doesn't seem to have a /force or similar switch. Is there any way to show hidden files/folders?
Does not appear to be a way no. Can't even find anything authoritative that specifically states this is normal behaviour
The capitalized "Tree" doesn't work on Linux, it's just "tree"
9

The following script will show the tree as a window, it can be added to any form present in the script

function tree {

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

   # create Window
   $Form = New-Object System.Windows.Forms.Form
   $Form.Text = "Files"
   $Form.Size = New-Object System.Drawing.Size(390, 390)
   # create Treeview-Object
   $TreeView = New-Object System.Windows.Forms.TreeView
   $TreeView.Location = New-Object System.Drawing.Point(48, 12)
   $TreeView.Size = New-Object System.Drawing.Size(290, 322)
   $Form.Controls.Add($TreeView)

   ###### Add Nodes to Treeview
   $rootnode = New-Object System.Windows.Forms.TreeNode
   $rootnode.text = "Root"
   $rootnode.name = "Root"
   [void]$TreeView.Nodes.Add($rootnode)

   #here i'm going to import the csv file into an array
   $array=@(Get-ChildItem -Path D:\personalWorkspace\node)
   Write-Host $array
   foreach ( $obj in $array ) {                                                                                                             
        Write-Host $obj
        $subnode = New-Object System.Windows.Forms.TreeNode
        $subnode.text = $obj
        [void]$rootnode.Nodes.Add($subnode)
     }

   # Show Form // this always needs to be at the bottom of the script!
   $Form.Add_Shown({$Form.Activate()})
   [void] $Form.ShowDialog()

   }
   tree

2 Comments

What's with the local path D:\personalWorkspace\node ?
It is a folder location whose files are display
7

In Windows, navigate to the directory of interest

Shift+ right click mouse -> Open PowerShell window here

Get-ChildItem | tree /f > tree.log

Comments

4

The best and clear way for me is:

PS P:\> Start-Transcript -path C:\structure.txt -Append
PS P:\> tree c:\test /F
PS P:\> Stop-Transcript

Comments

1

Here's a PowerShell script to list folders and files, color files differently than folders, and adding file size as well as folder size:

function Get-FolderTreeWithSizes {
    param (
        [string]$Path = ".",
        [int]$Indent = 0
    )

    $items = Get-ChildItem -Path $Path -Force
    $folderSize = 0

    foreach ($item in $items) {
        if ($item.PSIsContainer) {
            $subFolderSize = (Get-ChildItem -Path $item.FullName -Recurse -Force | Measure-Object -Property Length -Sum).Sum
            $subFolderSizeKB = "{0:N2} KB" -f ($subFolderSize / 1KB)
            Write-Host (" " * $Indent + "+-- " + $item.Name + " [Folder] (" + $subFolderSizeKB + ")") -ForegroundColor Green
            Get-FolderTreeWithSizes -Path $item.FullName -Indent ($Indent + 4)
        } else {
            $fileSizeKB = "{0:N2} KB" -f ($item.Length / 1KB)
            Write-Host (" " * $Indent + "+-- " + $item.Name + " (" + $fileSizeKB + ")") -ForegroundColor Yellow
            $folderSize += $item.Length
        }
    }

    if ($Indent -eq 0) {
        Write-Host ("Total Size of '$Path': {0:N2} KB" -f ($folderSize / 1KB)) -ForegroundColor Cyan
    }
}

Get-FolderTreeWithSizes

Example output:

enter image description here

Comments

0

You can use command Get-ChildItem -Path <yourDir> | tree >> myfile.txt this will output tree-like structure of a directory and write it to "myfile.txt"

4 Comments

That does the same thing as "tree", and only gives me the directory structure. How can I add the files to that tree?
Tree will have output but it will be from the current directory not <yourDir>
@Matt I've checked in such way and thought it will work in other cases
I tried "Get-ChildItem | tree" and got the same results as "tree". I have PowerShell 2.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.