This is done by getting to the same resources via a different url - raw.githubusercontent.com (hosted by github not a third-party). If it is a private repository, you'll need:
- a personal access token referenced in the header of the request
- the project path on github
- the name of the script you're trying to get
To jump straight to the PowerShell code needed, it would be-
$baseurl = "https://raw.githubusercontent.com/yourproject/subdir/master"
$tokenheaders = @{"Authorization"="token tokenyougeneratedforprivateprojectaccess";"Accept"= "application/vnd.github.v3.raw"}
$scripturl="$baseurl/yourscript.ps1"
Invoke-WebRequest -Uri $scripturl -Headers $tokenheaders -UseBasicParsing |Select-Object -ExpandProperty Content
This will get you the raw code, not a file, and this is using the deprecated v3 of the api, not v4 (but is still working for me as of this writing and should for at least a few more years).