Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

help formatting the output of a script....

help formatting the output of a script....

help formatting the output of a script....

I need to write a PS script that outputs process tree with child processes nested under the parent processes. It needs to identify all processes with no parent processes as the root for the trees.

I am NOT a PS programmer, not even a little bit so I need assistance please. Found some examples that kinda give me what I am looking for but need some guidance...:

CODE --> powershell

$ProcessesById = @{}
foreach ($Process in (Get-WMIObject -Class Win32_Process)) {
  $ProcessesById[$Process.ProcessId] = $Process

$ProcessesWithoutParents = @()
$ProcessesByParent = @{}
foreach ($Pair in $ProcessesById.GetEnumerator()) {
  $Process = $Pair.Value

  if (($Process.ParentProcessId -eq 0) -or !$ProcessesById.ContainsKey($Process.ParentProcessId)) {
    $ProcessesWithoutParents += $Process

  if (!$ProcessesByParent.ContainsKey($Process.ParentProcessId)) {
    $ProcessesByParent[$Process.ParentProcessId] = @()
  $Siblings = $ProcessesByParent[$Process.ParentProcessId]
  $Siblings += $Process
  $ProcessesByParent[$Process.ParentProcessId] = $Siblings

function Show-ProcessTree([UInt32]$ProcessId, $IndentLevel) {
  $Process = $ProcessesById[$ProcessId]
  $Indent = " " * $IndentLevel
  if ($Process.name) {
    $Description =  $Process.name
  } else {
    $Description = $Process.Caption

  Write-Output ("{0,6}{1} {2}" -f $Process.ProcessId, $Indent, $Description)
  foreach ($Child in ($ProcessesByParent[$ProcessId] | Sort-Object CreationDate)) {
    Show-ProcessTree $Child.ProcessId ($IndentLevel + 4)

Write-Output ("{0,6} {1}" -f "Id", "Process Name")
Write-Output ("{0,6} {1}" -f "---", "------------")

foreach ($Process in ($ProcessesWithoutParents | Sort-Object CreationDate)) {
  Show-ProcessTree $Process.ProcessId 0

Processes need to be identified at the root of trees and nest children under parents. I have attached an example picture of what the output should look like: http://files.engineering.com/getfile.aspx?folder=0...

Thank you!

RE: help formatting the output of a script....

Are you even getting the correct data to output? Please post what you have in each of your arrays.

And what is this section of code suppose to do? Because about 9 lines above it you declare $ProcessesByParent
as a hashtable.


if (!$ProcessesByParent.ContainsKey($Process.ParentProcessId)) {
    $ProcessesByParent[$Process.ParentProcessId] = @()
  $Siblings = $ProcessesByParent[$Process.ParentProcessId]   \
  $Siblings += $Process                                       >--- What is this doing?
  $ProcessesByParent[$Process.ParentProcessId] = $Siblings   / 

Light travels faster than sound. That's why some people appear bright until you hear them speak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close