Using PowerShell to download file from private GitHub repository (using OAuth)

I have searched extensively for a solution but have yet to find success. I just want to be able to download files from my private GitHub repo using PowerShell. I want to use OAuth, not basic auth, so I have generated a token. But from here, none of the examples I’ve referenced worked for me. Best I could do was get a “Not Found” response.

An example of code I’ve tried is:

Invoke-WebRequest -Headers @{"Authorization"="token 123456789012345678901234567890"} -OutFile C:\Temp\MyFile.txt


Invoke-WebRequest : {“message”:“Not Found”,“documentation_url”:“”}

I’m fairly confident that I have the authentication right. I believe I just have the path wrong path to my file. Any help would be greatly appreciated.

Hey there @RaySmalley thanks for the post, and welcome! Hope that you can find answers to your questions and hopefully here at least, I can provide one. :crossed_fingers:

There are at least a couple things to consider, I think.

  1. The media type Header – you’ll want to use either application/vnd.github.VERSION.raw or application/vnd.github.VERSION.html
  2. Invoke-RestMethod I believe is the appropriate Ps method for this curl equivalent, instead of Invoke-WebRequest

I’ve used Postman to generate this Powershell script that may be helpful for you:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "token 123456789012345678901234567890")
$headers.Add("Accept", "application/vnd.github.VERSION.raw")

$response = Invoke-RestMethod '{owner}/{repo}/contents/filename.txt' -Method 'GET' -Headers $headers -Body $body
$response | ConvertTo-Json

You’ll of course need to add a valid path to your file, and apply your Personal Access Token, to the Authorization header, and include your -OutFile preference. I didn’t have a Windows environment to test this in, but Postman was able to use this query to successfully retrieve my file’s contents.

If that doesn’t work, please let us know! If you’re trying to query from a public repository, a link to the repo would be incredibly helpful.