Today I had to create tree-like structure from given CSV file. This is what I received:

parent,child
0,a
0,b
0,c
a,d
d,e
d,f
e,g
b,h
c,i
c,j

To see its “visual” representation I created small function called Get-Child (maybe better to name it Get-Tree).

function Get-Child
{
    [CmdletBinding()]

    param(
        [Parameter(
            Mandatory = $true,
            Position = 0
        )]
        $InputObject
        ,
        [Parameter(
            Mandatory = $true,
            Position = 1
        )]
        [string]
        $Id
        ,
        [Parameter()]
        [int]
        $Shift = 0
        ,
        [Parameter()]
		[string]
		$RootName
    )

    if ($PSBoundParameters.ContainsKey('RootName')) { $RootName }
    else { if ($shift -eq 0) { $id } }

    $indent = ' ' * $shift
    $InputObject |
        Where { $_.Parent -eq $id } |
        ForEach { "{0} {1}" -f $indent, $_.Child;
            Get-Child -InputObject $InputObject -id $_.Child -shift $($shift+2)
        }
}

To see how it works, let’s create variable with data provided:

$d = @'
parent,child
0,a
0,b
0,c
a,d
d,e
d,f
e,g
b,h
c,i
c,j
'@ | ConvertFrom-Csv

Now it’s possible to create tree just by calling the function. As I needed to modify root name, I added support for RootName parameter.

PS> Get-Child -InputObject $d -Id 0
0
 a
   d
     e
       g
     f
 b
   h
 c
   i
   j
PS> Get-Child -InputObject $d -Id 0 -RootName ‘Root’
Root
 a
   d
     e
       g
     f
 b
   h
 c
   i
   j

If the parameter is not used, actual name of root is used, otherwise parameter value is used as the name for root.