Since a lot of people are starting to store their scripts and files in GitHub repositories, I often get the question, how can I download a script or a file from a Git repository using PowerShell. There are a couple of ways to you can obviously do this. You could use git and do a git clone of the repository and then run the script. In many cases however you don’t have git installed on the machine or it would be too much to clone the whole repo. So you can simply download the file or script from GitHub using PowerShell using the Invoke-WebRequest cmdlet.
Invoke-WebRequest -Uri https://raw.githubusercontent.com/thomasmaurer/demo-cloudshell/master/helloworld.ps1 -OutFile .\helloworld.ps1
This download my HelloWorld.ps1 PowerShell example file from the following example repository on GitHub.
You can also create the following one-liner which directly starts the PowerShell file using chaining in PowerShell.
Invoke-WebRequest -Uri https://raw.githubusercontent.com/thomasmaurer/demo-cloudshell/master/helloworld.ps1 -OutFile .\helloworld.ps1; .\helloworld.ps1
I hope this quick blog post on how to download scripts or files from GitHub using PowerShell was helpful. If you have any questions, feel free to leave a comment below.