How to output as csv from invoke-restmethod

%3CLINGO-SUB%20id%3D%22lingo-sub-999281%22%20slang%3D%22en-US%22%3EHow%20to%20output%20as%20csv%20from%20invoke-restmethod%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-999281%22%20slang%3D%22en-US%22%3E%3CP%3Eoutput%20result%20in%20csv%20format%20for%20Invoke-restmethod.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-999281%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-999833%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20output%20as%20csv%20from%20invoke-restmethod%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-999833%22%20slang%3D%22en-US%22%3E%3CP%3EReally%20depends%20on%20the%20output%20you%20are%20getting.%20Example%20query%20might%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1004381%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20output%20as%20csv%20from%20invoke-restmethod%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1004381%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F454072%22%20target%3D%22_blank%22%3E%40DeepakMhaskar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3Bsaid%2C%20it%20depends%20on%20the%20type%20of%20the%20response%20received.%3C%2FP%3E%3CP%3EIf%20you%20get%20a%20response%20in%20JSON%20format%2C%20the%20Powershell%20itself%20converts%20the%20JSON%20response%20into%20Powershell%20object%20(psobject)%2C%20if%20the%20returned%20Powershell%20object%20is%20array%2C%20you%20can%20simply%20export%20it%20using%20Export-Csv%20command.%3C%2FP%3E%3CPRE%3E%24apiUrl%20%3D%20%22https%3A%2F%2Fgraph.microsoft.com%2Fv1.0%2Fusers%22%0A%24response%20%3D%20Invoke-RestMethod%20-Headers%20%40%7BAuthorization%20%3D%20%22Bearer%20%24accessToken%22%7D%20-Uri%20%24apiUrl%20-Method%20Get%0A%0A%24users%20%3D%20%24response.value%0A%24users%20%7C%20Export-Csv%20-Path%20%22C%3A%5CUsers.csv%22%20-NoTypeInformation%0A%23%23%23%23%0A%24users%20%7C%20Select%20displayName%2CuserPrincipalName%20%7C%20Export-Csv%20-Path%20%22C%3A%5CUsers.csv%22%20-NoTypeInformation%3C%2FPRE%3E%3CP%3Eif%20your%20response%20is%20not%20in%20the%20required%20format%2C%20then%20you%20have%20to%20form%20required%20JSON%20string%20from%20the%20response%20and%20convert%20to%20ps%20object.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

output result in csv format for Invoke-restmethod.

2 Replies
Highlighted

Really depends on the output you are getting. Example query might help.

@DeepakMhaskar 

 

As @Vasil Michev said, it depends on the type of the response received.

If you get a response in JSON format, the Powershell itself converts the JSON response into Powershell object (psobject), if the returned Powershell object is array, you can simply export it using Export-Csv command.

$apiUrl = "https://graph.microsoft.com/v1.0/users"
$response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $apiUrl -Method Get

$users = $response.value
$users | Export-Csv -Path "C:\Users.csv" -NoTypeInformation
####
$users | Select displayName,userPrincipalName | Export-Csv -Path "C:\Users.csv" -NoTypeInformation

if your response is not in the required format, then you have to form required JSON string from the response and convert to ps object.