Building a PowerShell Process Tree

Your next PowerShell scripting challenge has arrived. Some of you may be familiar with the tree command-line tool. If you’ve never seen it, try tree $home at a PowerShell prompt. On non-Windows machines you may need to install an additional package. The point of the command is to graphically display hierarchical content from a command prompt.

Since processes can have child processes there is an implied hierarchy. The Chairman would like you to write a PowerShell equivalent of the tree command but for processes.

Intermediate Requirements

  • The topmost level should be the computer name.
  • At a minimum, you should display a tree version using the process name.
  • Use ASCII characters to display the “tree” branches.

Advanced Requirements

Once you’ve met the above requirements see how many of these items you can accomplish.

  • Query remote computers.
  • Use ANSI or extended characters for the tree.
  • Include the ProcessID or other user-specified properties.
  • Take into account orphaned processes – those whose parent is no longer running.
  • Colorize output on criteria of your choice such as run time, company name, user ID, or Working Set size.

Bonus Elements

Adding any of these features will truly set your code apart.

  • Allow some type of filtering for the associated user name.
  • Allow some type of sorting such as by name or start time,
  • Export the graphical tree to an HTML file,
  • Work cross-platform.

As always, please post links to your work and solutions. Good luck!


5 Replies to “Building a PowerShell Process Tree”

    • Scott

      I was having a hard time with this. Was trying to loop thru all procs but could not find a way to trace back all parents. I could get 2 levels but then my method broke. Your use of the generic.List has taught me something. I am self taught, so I’m not always familiar with the .net uses. Thanks for your example!

  1. Russell Roefer

    Late, but still fun. I explored using more helper functions with this than I normally do, but it seemed to make more sense to me this way. Hopefully it’ll make sense to you all as well. At the moment, this works locally and remotely, outputs the tree, adds ANSI red color for orphaned processes, and allows the user to add more properties to show. I might go back in to add more colors for other properties or explore an export to HTML. For now, this is enough fun.

    https://github.com/rgroefer/ProcessTree

Comments are closed.