PowerShell - Listing all Folders, Subfolders and each contained files (recursive) but in a formatted way (Tree-View)

I use the following commands on PowerShell to create a list of all files and subfolders within a specific directory:

get-childitem -path c:\users\username\desktop\test -recurse | select name

So assuming I have a folder called "test" on my desktop and within this folder I have three files and one subfolder, which itself contains further files and subfolders and so on, I do get something like this as an output:

subfolder 1 of "test"
file 1 in "test"
file 2 in "test"
file 3 in "test"
subfolder a of "subfolder 1"
file 1 in subfolder 1
file 2 in subfolder 1
file 3 in subfolder 1
file 1 in subfolder a
file 2 in subfolder a
file 3 in subfolder a

So this is nice but I would like to get another kind of output, something looking like this:

+  c:\users\username\desktop\test
|  -  file 1 in "test"
|  -  file 2 in "test"
|  -  file 3 in "test"
|--+  subfolder 1 of "test"
|  |  -  file 1 in subfolder 1
|  |  -  file 2 in subfolder 1
|  |  -  file 3 in subfolder 1
|  |--+  subfolder a of "subfolder 1"
|  |  |  -  file 1 in subfolder a
|  |  |  -  file 2 in subfolder a
|  |  |  -  file 3 in subfolder a
|--+  subfolder 2 of "test"
|  |  -
|  |  .
|  .  .
.  .
.

Is it possible (and if yes - how then?) to get an output looking like this?

I know there was a dos command called "tree" back then but it cannot work with the output of get-childitem in PowerShell due to its limitations. Is there some kind of equivalent command in PowerShell or can I do this with the get-childitem command and its switches / additions / ... ?

Sorry for my bad English. And: Sorry, I am a total beginner on PowerShell.

Upvotes: 0

Views: 18481

Answers (2)

postanote
postanote

Reputation: 16096

You can call any cmd/DOS executable from Powershell. As long as you do so properly. IN the consolehost (powershell.exe/pwsh.exe), it's virtually the same as using cmd.exe, but from the ISE it's a bit different. YOu can't use interactive commands in the ISE. You can use the command, but you must pass t al it needs.

In the PowerShell consolehost (powershell.exe/pwsh.exe), just type...

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.1}
BuildVersion                   10.0.19041.1
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


tree |more
Folder PATH listing for volume Data
Volume serial number is CE3D-F392
D:.
├───.vs
│   └───Scripts
│       └───v16
├───.vscode
...

PowerShell: Running Executables

No reason to start this from scratch. There are plenty of examples and even modules that provide this capability.

A quick search would show you start with a tweak or use as they are...

'PowerShell treeView':

PowerTip: View Directory List as Tree by Using PowerShell Use the Show-Tree cmdlet from the PowerShell Community Extension Project:

Find-Module -Name pscx | 
Format-Table -AutoSize
# Results
<#
Version Name Repository Description                                                                                          
------- ---- ---------- -----------                                                                                          
3.3.2   Pscx PSGallery  PowerShell Community Extensions (PSCX) base module which implements a general-purpose set of Cmdlets.
#>

Show-Tree e:\data –depth 2

https://serverfault.com/questions/744660/powershell-populating-treeview-with-directory-hierarchy

$objDriveLetters = GET-WMIOBJECT –query "SELECT * from win32_logicaldisk"
$form = New-Object System.Windows.Forms.Form
$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

foreach ($iDrive in $objDriveLetters)
    {
        $DriveRoot = Get-Item $iDrive.DeviceID
        #$FolderRoot = Get-ChildItem -Path $iDrive.DeviceID
        $FolderRoot = Get-Item -Path $iDrive.DeviceID
        $treeView.Nodes.Add($FolderRoot.FullName, $FolderRoot.FullName)
    }

$form.Controls.Add($treeView)
$form.ShowDialog()

Create A File System Size Tree View Using PowerShell:

https://key2consulting.com/powershell-file-directory-tree-view

    #Variables that need to be set for each run
    $startFolder = "C:\Program Files"; #The starting folder to analyze
    $sourceHTMLFile = "C:\finalTemplate.html"; #The html source template file
    $destinationHTMLFile = "C:\final.html"; #The final html file that will be produced, #does not need to exist

    $htmlLines = @();

    #Function that creates a folder detail record
    function CreateFolderDetailRecord
    {
        param([string]$FolderPath)
    
        #Get the total size of the folder by recursively summing its children
        $subFolderItems = Get-ChildItem $FolderPath -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
        $folderSizeRaw = 0;
        $folderSize = 0;
        $units = "";

        #Account for no children
        if($subFolderItems.sum -gt 0)
        {
            $folderSizeRaw = $subFolderItems.sum;     
        }    

        #Determine units for a more friendly output
        if(($subFolderItems.sum / 1GB) -ge 1)
        {
            $units = "GB"
            $folderSize = [math]::Round(($subFolderItems.sum / 1GB),2)
        }
        else
        {
            if(($subFolderItems.sum / 1MB) -ge 1)
            {
                $units = "MB"
                $folderSize = [math]::Round(($subFolderItems.sum / 1MB),2)
            }
            else
            {
                $units = "KB"
                $folderSize = [math]::Round(($subFolderItems.sum / 1KB),2)
            }
        }

        #Create an object with the given properties
        $newFolderRecord = New-Object –TypeName PSObject
        $newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderPath –Value $FolderPath;
        $newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeRaw –Value $folderSizeRaw
        $newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeInUnits –Value $folderSize;
        $newFolderRecord | Add-Member –MemberType NoteProperty –Name Units –Value $units;

        return $newFolderRecord;
    }

    #Function that recursively creates the html for the output, given a starting location
    function GetAllFolderDetails
    {
        param([string]$FolderPath)    

        $recursiveHTML = @();

        #Get properties used for processing
        $folderItem = Get-Item -Path $FolderPath
        $folderDetails = CreateFolderDetailRecord -FolderPath $FolderPath
        $subFolders = Get-ChildItem $FolderPath | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object

        #If has subfolders, create hmtl drilldown. 
        if($subFolders.Count -gt 0)
        {
            $recursiveHTML += "<li><span class='caret'>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)" + "</span>"
            $recursiveHTML += "<ul class='nested'>"
        }
        else
        {
            $recursiveHTML += "<li>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)";
        }

        #Recursively call this function for all subfolders
        foreach($subFolder in $subFolders)
        {
            $recursiveHTML += GetAllFolderDetails -FolderPath $subFolder.FullName;
        }

        #Close up all tags
        if($subFolders.Count -gt 0)
        {
            $recursiveHTML += "</ul>";
        }

        $recursiveHTML += "</li>";
    
        return $recursiveHTML
    }

    #Processing Starts Here

    #Opening html
    $htmlLines += "<ul id='myUL'>"

    #This function call will return all of the recursive html for the startign folder and below
    $htmlLines += GetAllFolderDetails -FolderPath $startFolder

    #Closing html
    $htmlLines += "</ul>"

    #Get the html template, replace the template with generated code and write to the final html file
    $sourceHTML = Get-Content -Path $sourceHTMLFile;
    $destinationHTML = $sourceHTML.Replace("[FinalHTML]", $htmlLines);
    $destinationHTML | Set-Content $destinationHTMLFile 

Upvotes: 1

OwlsSleeping
OwlsSleeping

Reputation: 1570

The old "Tree" you're used to from cmd is an application in your system32 folder rather than some hard-coded cmd functionality.

So you can still run it from powershell as usual.

e.g.

Tree 'C:\Foldername'

Robocopy and some other well known applications work the same way.

Errors in external programs can be captured in $LastExitCode rather than the usual $Error. What those codes mean will vary depending on the program.

Upvotes: 2

Related Questions