Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell script to export Array in CSV format with Headers

I’ve below PS Script which connects to API and puts responses on an Array List.
I’m struggling to convert Array List to CSV file and giving them a Column Header.
Anyone got Idea how to this can be done on PowerShell?


$headers.Add("Authorization", "Bearer $access_token")

$response = Invoke-RestMethod -Uri $URL -Method GET -Headers $headers -DisableKeepAlive -Certificate $certx
$js1 = $response.workers.person.legalName.givenName| ConvertTo-Json -depth 1 
$js2 = $response.workers.person.legalName.familyName1| ConvertTo-Json -depth 1
$js3 = $response.workers.businessCommunication.emails.emailUri| ConvertTo-Json -depth 1 



@($js1;$js2;$js3) | Out-File "C:\Users\file2.json"  ```

Output of above script is as below
[
    "a",
    "b",
    "c"
]
[
    "d",
    "e",
    "f"
]
[
    "g",
    "h",
    "i"
]

However i would like to have output something like below.

CSV File

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Seems like combining the different property values into a single object should do the trick:

$response.workers | ForEach-Object {
    [pscustomobject]@{
        Name    = $_.person.legalName.givenName
        Surname = $_.person.legalName.familyName1
        Email   = $_.businessCommunication.emails.emailUri
    }
} | Export-Csv path\to\file.csv -NoTypeInformation
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading