need help in storing output of for each loop into a variable and export to csv file.

Below is my script

        $disks = Get-AzDisk
        Foreach($disk in $disks)
             if($disk.ManagedBy -eq $VM.Id)
             $diskname = $disk.Name
             $encryptionsettings = $disk.EncryptionSettingsCollection
             if($encryptionsettings -eq $null)
             $encryption = "Not encrypted"
             $encryption = "Encrypted"
$VM | Select-Object @{Name="Disk Name";Expression={$diskname}},
                            @{Name="Encryption Status";Expression={$encryptionstatus}} | Export-Csv -Path $VMDetails -Delimiter "," -NoTypeInformation -Append
I need output as 
Disk Name  Encryption status
test , test1 , test2 , test3 Encrypted , Not encrypted , Encrypted , Encrypted
But I am getting output as below. 
Disk Name Encryption Status
test Not encrypted
test_DataDisk_0 Encrypted


test_OsDisk_1_aa301d403d1e46f6b2af9a49a0f46052 Encrypted
  • <#
    You have to collect that information as arrays, to get close to what you are after,
    You really will have issues, trying to do an Export-CSV, on a nest set of CSV, data, meaning, header and single property arrays liek you are.

    Anytime you are dealing with collections, in a variable, they get unwieldy when you try and output.
    CSV by default are comma separate, thus the layout you are after would require a different approach to be implemented.

    For example:

    # Using my local system, since I have no AZDisks.
    $DiskNumber = @()
    $HealthStatus = @()

    Get-Disk |
        $DiskReport   = [PSCustomObject]@{
            DiskName  = $DiskNumber += $PSItem.Number
            Status    = $HealthStatus += $PSItem.HealthStatus


    $DiskReport | Out-File -FilePath 'E:\Temp\DiskReport.txt'
    Import-Csv -Path 'E:\Temp\DiskReport.txt'

    # Results

    DiskName        Status                                      
    --------        ------                                      
    {1, 0, 2, 3, 4} {Healthy, Healthy, Healthy, Healthy, Healthy}