I need a way to export all Azure Ad groups with their corresponding owner to a csv file. The below code works, but the formatting of the csv file is horrendous. Everything is in one column and hard to read. How would I get all groups in one Column and the corresponding owner in a separate column in the corresponding row. Any help would be appreciated

```
 $groups=Get-AzureADGroup -All $true
    ForEach ($group in $groups){
        $Owners = Get-AzureADGroupOwner -ObjectId $group.ObjectId -All $true 
        ForEach ($Owner in $Owners){ 
            Write-output $group.DisplayName "," $Owner.ObjectId "," $Owner.ObjectType $Owner.UserType "," $Owner.UserPrincipalName >> C:\scripts\Owner.csv
        }
    } 

```

Updated Script

```
$array = @()
$Properties=@{}
$Properties.add("GroupDisplayName","1")
$Properties.add("OwnerObjectId","2")
$Properties.add("OwnerObjectType","3")
$Properties.add("OwnerUserType","4")
$Properties.add("OwnerUserPrincipalName","5")
$groups = Get-AzureADGroup -All $true
Foreach($group in $groups){

     $Owners = Get-AzureADGroupOwner -ObjectId $id  -All $true
     $Properties.GroupDisplayName=$group.DisplayName

     if($Owners -ne $null){
       # group has owner
        Foreach($Owner in $Owners){

                $Properties.OwnerObjectId=$Owner.ObjectId
                $Properties.OwnerObjectType=$Owner.ObjectType
                $Properties.OwnerUserType=$Owner.UserType
                $Properties.OwnerUserPrincipalName=$Owner.UserPrincipalName
                $obj=New-Object PSObject -Property $Properties
                $array +=$obj 

        }
     }
     else{
                #group has no owner
                $Properties.OwnerObjectId=$null
                $Properties.OwnerObjectType=$null
                $Properties.OwnerUserType=$null
                $Properties.OwnerUserPrincipalName=$null
                $obj=New-Object PSObject -Property $Properties
                $array +=$obj  

     }

}
$array | export-csv -Path C:\test1234.csv -NoTypeInformation -Encoding UTF8

```