TreeView with dynamically populating child nodes with children (Active Directory related)

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
NewRootUsr
Posts: 10
Last visit: Wed Mar 13, 2024 2:16 pm

TreeView with dynamically populating child nodes with children (Active Directory related)

Post by NewRootUsr »

I am currently working on a project and it involves Active Directory, it is a PowerShell GUI TreeView based AD explorer, I am stuck attempting to populate child nodes, but to do it dynamically and the documentation that I have read either doesn't support what I am attempting to achieve, or something is going over my head.

Right now I have a single function that works as I expect to populate a root node with children gathered from AD , what I am attempting to do next is to populate each of these children with their children, I thought I could do achieve this by using

Code: Select all

treeview1.Nodes[1].Nodes[$TopLvlOU].Add("$Children")
or similar, but needing to use a value within [] causes me a headache as I would need to determine each value as the tree goes on, and since this is meant to be dynamic I am not sure how to achieve this, I did see there is a find node, but that did not return the value of the node which would be usable.

The overall goal is to populate all OU's within our AD as they display in the AD Users & Computers tool, but in a friendly PowerShell GUI which will have buttons and such, but ideally dynamic as we need/remove OU's or re-arrange etc.
Code: [Select all] [Expand/Collapse] [Download] (BuildADTree.ps1)
  1. Function BuildADTree
  2. {
  3.     # Get the Top level AD structure as variable
  4.     $TopAD = Get-ADOrganizationalUnit -SearchBase $BaseADOU -Filter * -SearchScope OneLevel | Select-Object -ExpandProperty Name
  5.     Foreach ($TopLvlOU in $TopAD)
  6.     {
  7.         #Write-Host "$TopLvlOU"
  8.        
  9.         # Build the tree based off of node 1 (AD Base )
  10.         $treeview1.Nodes[1].Nodes.Add("$TopLvlOU");
  11.     }
  12. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView with dynamically populating child nodes with children (Active Directory related)

Post by jvierra »

The attached is the best, fastest and easiest way to load a TreeView from AD and generate the hierarchy. Notice it uses almost no code and uses the hierarchy of AD and the hierarchy of the TV to work synchronously to create the tree.

This form should work without changes on any AD client computer.
Attachments
Demo-ADSITreeView.psf
(23.67 KiB) Downloaded 126 times
NewRootUsr
Posts: 10
Last visit: Wed Mar 13, 2024 2:16 pm

Re: TreeView with dynamically populating child nodes with children (Active Directory related)

Post by NewRootUsr »

jvierra wrote: Sat Aug 06, 2022 4:23 pm The attached is the best, fastest and easiest way to load a TreeView from AD and generate the hierarchy. Notice it uses almost no code and uses the hierarchy of AD and the hierarchy of the TV to work synchronously to create the tree.

This form should work without changes on any AD client computer.
This is awesome, I'm taking a look to see how to best use this new info. Thanks. You wouldn't happen to know anything about utilizing images within treeview and nodes, trying to make things easier to navigate by adding OU folders I am trying to determine how add an image file (icon) to the associated OU's that get populated.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: TreeView with dynamically populating child nodes with children (Active Directory related)

Post by jvierra »

Here is an example of how to use a Treeview with images. It also is a good sampler of other TreeView options.
Attachments
Demo-TreeviewFolders.psf
(31.52 KiB) Downloaded 118 times
This topic is 1 year and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked