Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Created December 6, 2016 21:21
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save chrisbrownie/f20cb4508975fb7fb5da145d3d38024a to your computer and use it in GitHub Desktop.
Save chrisbrownie/f20cb4508975fb7fb5da145d3d38024a to your computer and use it in GitHub Desktop.
PowerShell function to download files from a GitHub repository
function DownloadFilesFromRepo {
Param(
[string]$Owner,
[string]$Repository,
[string]$Path,
[string]$DestinationPath
)
$baseUri = "https://api.github.com/"
$args = "repos/$Owner/$Repository/contents/$Path"
$wr = Invoke-WebRequest -Uri $($baseuri+$args)
$objects = $wr.Content | ConvertFrom-Json
$files = $objects | where {$_.type -eq "file"} | Select -exp download_url
$directories = $objects | where {$_.type -eq "dir"}
$directories | ForEach-Object {
DownloadFilesFromRepo -Owner $Owner -Repository $Repository -Path $_.path -DestinationPath $($DestinationPath+$_.name)
}
if (-not (Test-Path $DestinationPath)) {
# Destination path does not exist, let's create it
try {
New-Item -Path $DestinationPath -ItemType Directory -ErrorAction Stop
} catch {
throw "Could not create path '$DestinationPath'!"
}
}
foreach ($file in $files) {
$fileDestination = Join-Path $DestinationPath (Split-Path $file -Leaf)
try {
Invoke-WebRequest -Uri $file -OutFile $fileDestination -ErrorAction Stop -Verbose
"Grabbed '$($file)' to '$fileDestination'"
} catch {
throw "Unable to download '$($file.path)'"
}
}
}
@bytekoder
Copy link

How will this work if the repository is private? Is there anyone to stick in the oauth token?

@0fflineDocs
Copy link

Working like a charm, well done!

@zerotag
Copy link

zerotag commented Jun 15, 2019

I know it's been three years, but I forked your git, and added authentication (to increase API limit) and some minor tweaks.
Would love to hear what you think. Gist Fork

@kapilkhub88
Copy link

Tried this script for download a particular file kept at private repo but did not work. It ran immediately and did not show execute any exception scenarios as well. Is there any environment pre-requisites that needs to be met or am I missing something here ?

@RaySmalley
Copy link

RaySmalley commented Aug 19, 2020

How will this work if the repository is private? Is there anyone to stick in the oauth token?

I need help with this too.

@StanleyGoldman
Copy link

If the repo is private...

$token = "<Create a Personal Access Token>"
$headers = @{Authorization = "token $($token)"}
Invoke-WebRequest -Headers $headers -Uri $uri

@Doug-Moody
Copy link

If the repo is private...

$token = "<Create a Personal Access Token>"
$headers = @{Authorization = "token $($token)"}
Invoke-WebRequest -Headers $headers -Uri $uri

Thank you @StanleyGoldman , So difficult to find this specific scenario cleanly presented.

@marchro
Copy link

marchro commented Mar 16, 2021

I am new on GitHub. This article was very useful. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment