First off, this is a really bad idea most of the time. You shouldn’t run scripts on your system you don’t understand/haven’t reviewed and that someone else could edit at any time.
However, it is possible to pull scripts directly from GitHub and run them on a system. Allowing you to use 2 simple lines to invoke a larger script, and to pull the latest version of a script every time.
What we are doing here is using Invoke-WebRequest to get the GitHub “RAW” file from the master branch, which should always be the “current” script, then we are using Invoke-Expression to run that Script
$ScriptFromGitHub = Invoke-WebRequest https://raw.githubusercontent.com/tomarbuthnot/Run-PowerShell-Directly-From-GitHub/master/Run-FromGitHub-SamplePowerShell.ps1
In this case, the script we are pulling is: https://github.com/tomarbuthnot/Run-PowerShell-Directly-From-GitHub , which is just a little sample script
- Anyone who has the rights to change the source on GitHub could change it at any time. This means you always pull the latest script, but also means they could change the script to format your C:\ Drive, so think carefully about if/how you use this
If you see this error when you try to do this:
It might be that GitHub has inserted a “.” before your first line, you can remove it with the GitHub web editor.