I need to grab a single file from bitbucket (git) using powershell. Complicating matters I need to do so over http/https as the system I'm running powershell from does not have direct internet access (therefore ssh isn't an option). How can I do so?

I thought that perhaps I could grab it using Invoke-WebRequest and accessing the raw URL. For example: https://bitbucket.org/company/project/raw/HEAD/some/special/file.txt

But it appears to access that you have to authenticate. I've tried setting up authentication but I can't get it working.

I thought next I could just pull it from git using git archive: git archive --remote=git://bitbucket.org/Company/project.git HEAD:some/path/file.txt but that also doesn't seem to work.

So how can I get a single file. I don't want to just do git clone because then I have to pull down (and store) 500MB worth of other files I don't care about in the repo which is an issue for two reasons

1) I have to have space to store all that data (at least temporarily)
2) Unless I want to tie up that storage permanently I have to re-download it each time I need the file. 

The file I need is 10KB so to tie up 500MB for a 10kb file seems absurd.

What's the answer? There's got to be a way to get Invoke-WebRequest to authenticate properly or just download a single file? Can any git gurus - particularly with powershell/windows backgrounds help?

Thanks! Brad


BitBucket appears to have a pretty standard REST API you can use with Invoke-WebRequest or Invoke-RestMethod.

Here are some links to their documentation: Authentication and GET raw content. The gist of those doc pages is that they support basic authentication and GET requests for raw file contents. So from your example, you'd need to do something like this:

# create a credential variable for your account
$cred = Get-Credential

# modify the base URL to hit the API endpoint instead of the standard web endpoint, pass the credentials with the request, and send the output to a file
Invoke-RestMethod -Credential $cred -Uri https://api.bitbucket.org/1.0/repositories/company/project/raw/HEAD/some/special/file.txt -OutFile .\file.txt
  • Awesome! Thank you so much. I saw the web api documents but I couldn't work out if it was possible to retrieve a file using them. Let me give that a try and i'll follow up. – Brad May 30 '17 at 14:29
  • That works perfectly - thank you! I was banging my head off the wall trying to figure out how to do that :) – Brad May 30 '17 at 18:45
  • I tried wget, curl, Invoke-WebRequest but they all did not work, Invoke-RestMethod was the right method :) – dr pain Mar 2 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.