Bookmarks merge from chrome into edge

Hi, I have a script that merges bookmars and folders from chrome to edge. The only issue is it migrates folders twice. Can you help to fix it?

#Script to import Chrome bookmarks to MS Edge
function Get-ChildBooks($children){
    
    $out = @()
    foreach ($nbook in $children)
    {
         if($nbook.type -eq 'url'){
                    
                        $out += [PSCustomObject]@{
                        
                                date_added = $nbook.date_added
                                guid       = $nbook.guid
                                id         = $nbook.id
                                name       = $nbook.name
                                show_icon  = 'False'
                                source     = 'user_add'
                                type       = $nbook.type
                                url        = $nbook.url
                    
                        }
              }else{                        
                         $out += [PSCustomObject]@{
                        
                                children            = @([System.Array]$(Get-ChildBooks -children $nbook.children))
                                date_added          = $nbook.date_added
                                date_last_used       = $nbook.date_last_used
                                date_modified       = $nbook.date_modified
                                guid         = $nbook.guid
                                id         =   $nbook.id
                                name       = $nbook.name
                                source     = 'extension'
                                type        = 'folder'

                                }
            }
    }

    return $out
}


try{
    
    $ChromeBookMarks = Get-Content "$env:LOCALAPPDATA\Google\Chrome\User Data\*\bookmarks" -raw | ConvertFrom-Json

    $EdgeBookMarks = Get-Content "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\bookmarks" -raw | ConvertFrom-Json
    #$EdgeBookMarks = $id
    foreach ($item in @('bookmark_bar', 'other', 'synced'))
    {
        foreach($chromebook in $ChromeBookMarks){
            $folder = $chromebook.roots.$item
            if($folder.children.Count -gt 0){
                foreach ($book in $folder.children)
                {
                    
                    if($book.type -eq 'url' -and $book.url -in $EdgeBookMarks.roots.$item.children.url){
                        continue
                    }
                    if($book.type -eq 'url'){
                    
                        $EdgeBookMarks.roots.$item.children += [PSCustomObject]@{
                        
                                date_added = $book.date_added
                                guid       = $book.guid
                                id         = $book.id
                                name       = $book.name
                                show_icon  = 'False'
                                source     = 'user_add'
                                type       = $book.type
                                url        = $book.url
                    
                        }
                    }else{                        
                         $EdgeBookMarks.roots.$item.children += [PSCustomObject]@{
                        
                                children            = @([System.Array]$(Get-ChildBooks -children $book.children))
                                date_added          = $book.date_added
                                date_last_used       = $book.date_last_used
                                date_modified       = $book.date_modified
                                guid         = $book.guid
                                id         =   $book.id
                                name       = $book.name
                                source     = 'extension'
                                type        = 'folder'

                        }
                    }
                }
            }
         }
    }    
    
    $start = $false
    $process = Get-Process -Name msedge -ErrorAction SilentlyContinue
    if($null -ne $process){
        $process |Stop-Process -Force -ErrorAction SilentlyContinue
        $start = $true
    }
    [string]$Json = ConvertTo-Json -InputObject $EdgeBookMarks -Depth 10

    [int]$Indentation = 4

    if ($Json -notmatch '\r?\n') {
        $Json = ($Json | ConvertFrom-Json) | ConvertTo-Json -Depth 100
    }

    $indent = 0
    $regexUnlessQuoted = '(?=([^"]*"[^"]*")*[^"]*$)'

    $result = $Json -split '\r?\n' |
        ForEach-Object {
            if ($_ -match "[}\]]$regexUnlessQuoted") {
                $indent = [Math]::Max($indent - $Indentation, 0)
            }

            $line = (' ' * $indent) + ($_.TrimStart() -replace ":\s+$regexUnlessQuoted", ': ')

            if ($_ -match "[\{\[]$regexUnlessQuoted") {
                $indent += $Indentation
            }

            $line
        }

    $k = ""
    for ($i = 0; $i -lt $result.Count; $i++)
    { 
    $k += $result[$i] + [environment]::NewLine
        if($result[$i] -like '*"children":*'){
        #if($i -gt 5){break}
            $k = $k.trim() + " "
            if($result[$i+2].trim() -eq '],'){
                $k = $k + $result[$i+2].Trim() + [environment]::NewLine
                $i+=2
                continue
            }
            $k += $result[$i+1].trim() + [environment]::NewLine
            $i++
        }
    }
    $k = $k.Trim()
    Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\bookmarks*" -Force -ErrorAction SilentlyContinue
    
    $k | Set-Content "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\bookmarks" -Encoding UTF8 
    $k | Set-Content "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\bookmarks.mbak" -Encoding UTF8 
    
    Write-Host "Chrome bookmarks successfully imported to edge. Please start the edge. " -BackgroundColor Green -ForegroundColor White
}catch{
    Write-Host "Error: Failed to import chrome bookmarks. $($_.Exception.Message)" -BackgroundColor Red
}