Monday, October 31, 2022

Notes About PowerShell: Adding a TreeView to the GUI - Part 3

In Notes About PowerShell: Adding a TreeView to the GUI - Part 2 of this series, we had a complete set of four PowerShell scripts that together creates a GUI window that displays a Windows Forms TreeView, allowing the user to select a family member from a small family tree. In this post, we're going to replace that family tree with the computer's file system.

Let's make one quick modification to tinker.ps1 file so that our results are graphically displayed in addition to textually in the console. Add in the bolded code below:

   ...
   
If ($Win.DialogResult -eq "OK") {
    Write-Host("The OK button was pressed. The data retrieved are:")
    Write-Host("`t            Node: `"$($Node.Text)`".")
    Write-Host("`tPath to the Node: `"$($NodePath.Text)`".")
    [System.Windows.Forms.MessageBox]::Show("Results:`n`nNODE:`n `"$($Node.Text)`"`n`nPATH TO NODE:`n `"$($NodePath.Text)`"")

   ...

Give 'er a test spin.

Okay, on to putting the filesystem into a treeview. First, we'll need to have the drive letters of Windows. For experimentation/learning purposes, at a PowerShell prompt (not in your script), enter the following command, and you'll see results similar to the following:

PS C:\Users\acutech> Get-PSDrive

Name           Used (GB)     Free (GB) Provider      Root                                CurrentLocation
----           ---------     --------- --------      ----                                ---------------
Alias                                  Alias                                                                                                                                                                                     
C                  24.30         55.00 FileSystem    C:\                                 Users\acutech
Cert                                   Certificate   \
D                                      FileSystem    D:\                                                                                                                                                                         
Env                                    Environment                                                                                                                                                                               
Function                               Function                                                                                                                                                                                 
HKCU                                   Registry      HKEY_CURRENT_USER                                                                                                                                                           
HKLM                                   Registry      HKEY_LOCAL_MACHINE                                                                                                                                                         
Variable                               Variable                                                                                                                                                                                 
WSMan                                  WSMan                                                                                                                                                                                    


PS C:\Users\acutech> 

We're only interested in the filesystem drive letters, not the registry keys or certificates or etc. So let's put some limitations on the command:

PS C:\Users\acutech> Get-PSDrive -PSProvider FileSystem

Name           Used (GB)     Free (GB) Provider      Root                                CurrentLocation
----           ---------     --------- --------      ----                                ---------------
C                  24.30         55.00 FileSystem    C:\                                 Users\acutech
D                                      FileSystem    D:\


PS C:\Users\acutech>

Better. But all we really want is the drive letter itself:

PS C:\Users\acutech> (Get-PSDrive -PSProvider FileSystem).Root
C:\
D:\

PS C:\Users\acutech> (Get-PSDrive -PSProvider FileSystem).Name
C
D

Great! Both of these commands give us an array containing the filesystem drives. Let's load them up into the treeview.

Let's start with a reminder of our tinker_add_nodes.ps1 file:

$TreeView.Nodes.Add("John")
$TreeView.Nodes[0].Nodes.Add("Mary")
$TreeView.Nodes[0].Nodes.Add("Fred")
$TreeView.Nodes[0].Nodes[1].Nodes.Add("Delbert")
$TreeView.Nodes[0].Nodes.Add("Alvin")
$TreeView.Nodes.Add("William")
$TreeView.Nodes[1].Nodes.Add("Estelle")
$TreeView.Nodes[1].Nodes.Add("Angus")
$TreeView.Nodes[1].Nodes.Add("Eugene")
$TreeView.Nodes[1].Nodes.Add("Marvin")
$TreeView.SelectedNode = $TreeView.Nodes[0].Nodes[1].Nodes[0]
$Win.ActiveControl = $TreeView
$Node.Text = $TreeView.SelectedNode.Text

Delete all of that code; we're done with it. And replace it with this code:

$Drives = (Get-PSDrive -PSProvider FileSystem).Root        # Get the drive letters in an array named $Drives
foreach ($_ in $Drives) {                                  # For each drive letter in the array,
    $TreeView.Nodes.Add($_)                                #   add the drive letter to the treeview.
}

Open up .tinker.ps1 if you haven't already done so, and run it. You should get something like this:

This Document is Under Construction

No comments: