Tom Talks Microsoft Teams and Skype for Business thoughts and news

Run PowerShell Scripts Directly from GitHub with Two Lines

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
Invoke-Expression $($ScriptFromGitHub.Content)

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

image

Note:

  • 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:

image

It might be that GitHub has inserted a “.” before your first line, you can remove it with the GitHub web editor.

image

About the author

Tom Arbuthnot

Tom Arbuthnot is Principal Solutions Architect at Unified Communications specialist Modality Systems. He is a Microsoft Certified Master and MVP, blogger, has a regular podcast with UCToday at tomtalks.show and is a regular speaker at events including Microsoft TechEd and Ignite. He co-runs The Microsoft UC User Group London.

1 comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Tom Talks Microsoft Teams and Skype for Business thoughts and news