Tom Talks Microsoft Teams and Microsoft 365 news and opinions

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

A Microsoft MVP and Microsoft Certified Master, Tom Arbuthnot is Founder and Principal at Empowering.Cloud as well as a Solutions Director at Pure IP.

Tom stays up to date with industry developments and shares news and his opinions on his Tomtalks.blog, UC Today Microsoft Teams Podcast and email list. He is a regular speaker at events around the world.

1 comment

Leave a Reply

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

Tom Talks Microsoft Teams and Microsoft 365 news and opinions