Jack Smith
Jack Smith

Reputation: 63

export data to CSV file with custom format in powershell

I am trying to get the value and format in a certain way. I tried a few methods but it didn't work. I am new to Powershell and still learning.

Add-Content -Path data.csv -Value ‘"Type","Res","Size”'
$aList= @()

 $Accountinfo = Get-AzStorageAccount -ResourceGroupName $resName
        IF ($($Accountinfo).count -gt 0) {
            foreach ($accountiinform in $Accountinfo) {
                $Names = "Storage Account"
                $props = [ordered]@{
                    "Res" = $Names
                    "Name"     = $accountiinform.StorageAccountName
                }
                $aList += New-Object pscustomobject -Property $props
            }
        }
    # There are more if statement-checking more resources and adding the $props object to $alist

$aList | foreach { Add-Content -Path data.csv -Value $_ }

what data.csv has:

"Type","Res","Size"
@{Res=Storage Account; Name=dataStorage;}
@{Res=VM; Name=dataVM; Size=250 GB;}
@{Res=VM; Name=dataVM2; Size=500 GB;}

What I want:

"Type","Res","Size"
Storage Account, Name=dataStorage,
VM,dataVM, 250 GB
VM, dataVM2,500 GB

Upvotes: 2

Views: 137

Answers (1)

Mike Kennedy
Mike Kennedy

Reputation: 394

Please change the last line to:

Replace
$aList | foreach { Add-Content -Path data.csv -Value $_ }

With this:
$aList | Export-Csv -Path C:\Temp\data.csv -NoTypeInformation

Upvotes: 3

Related Questions