Forum Discussion
DeepakMhaskar
Nov 11, 2019Copper Contributor
How to output as csv from invoke-restmethod
output result in csv format for Invoke-restmethod.
gastone
Dec 13, 2025Brass Contributor
$user = "GastoneCanali"
$csvPath = ".\$user-repos.csv"
try {
$repos = Invoke-RestMethod -Uri "https://api.github.com/users/$user/repos" `
-Headers @{'Accept'='application/vnd.github+json'; 'X-GitHub-Api-Version'='2022-11-28'}
$repos | ForEach-Object {
[PSCustomObject]@{
Name = $_.name
Description = $_.description
Language = if($_.language){$_.language}else{"N/A"}
Stars = $_.stargazers_count
Forks = $_.forks_count
Updated = ([datetime]$_.pushed_at).ToString("yyyy-MM-dd")
URL = $_.html_url
}
} | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
Write-Host "Exported $(Import-Csv $csvPath | Measure-Object).Count repos to $csvPath" -ForegroundColor Green
}
catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}The process works because Invoke-RestMethod automatically converts a JSON API response into PowerShell objects. To get a CSV, you simply pass these objects to the Export-Csv cmdlet. Use the -NoTypeInformation parameter to create a clean file without the object type header. If you need to filter or reshape the data first, you can use Select-Object in the pipeline to choose specific properties before exporting.