Use PowerShell to generate a list of files and directories

醉酒当歌 提交于 2019-12-02 20:30:07

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.

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

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

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"

In Windows, navigate to the directory of interest

Shift+ right click mouse -> Open PowerShell window here

Get-ChildItem | tree /f > tree.log
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!