If you are new to PowerShell then you are probably ready to dive in and start experimenting with scripts and all kinds of cool learning resources.
If you ever download a script from an external source then you need to be aware of a few things. The first thing to remember is that there is a legitimate safety concern to running scripts, especially if you do not know the source.
Having said that, it is important to remember that the execution policy that Microsoft introduced with PowerShell is not a safety or security feature. Instead, we need to think of it as being a preference that can have effects on how your system interacts with PowerShell.
If you are in a hurry when trying to test, prototype and check your scripts, then it can be quite frustrating trying to figure out how to bypass the execution policy, or disable it entirely.
Before you get started, it is important to make sure that you understand what each script does before you run it. PowerShell is a very powerful tool, and like any tool it will do what it is told, no exceptions. (We take no responsibility for any loss or damage that you might incur by bypassing the execution policy)
So with that out of the way, lets look at what the execution policy is in the first place, what it does, and why you need to know about it.
What is the PowerShell Execution Policy Anyway?
Time for a little explanation. The execution policy is a built in setting that decides whether your system can run Powershell scripts in the first place.
The standard condition that a new Powershell environment is set to is “restricted”, which is a nice way of saying that Powershell script execution is disabled.
This means that if you accidentally run the wrong script, or at least try to, then it will not run. This feature has probably saved countless sys admins, pentesters and devs some serious headaches.
The execution policy is more of a preference that can be controlled by your system administrators if they deem it to be necessary than it is a safety setting.
Why the Need for Bypassing the Execution Policy?
Below are some other great things you can do with PowerShell:
- Automation: Running scripts, restarting services, network testing – its all possible with PowerShell.
- API Calls: PowerShell has the ability to make calls to the Windows API, as well as REST APIs, URIs and more.
- Containerized Execution: PowerShell can be run in a variety of ways without impacting local storage
- Scripting: The best reason to bypass the execution policy is so that you can run your scripts in the first place.
- Create Services and Apps: PowerShell has some great modules that will allow you to compile your scripts into EXE’s which can be run directly.
Now that we know the what and the why, we can learn about the how.
Let’s look at a few ways to check your current execution policy settings, and how you can bypass them to get some Powershell work done.
Let’s Check out our Execution Policy
Now that we have decided that we would like to try out a few scripts we need to check if we are even able to run any of them in Powershell. To do this we have to run a cmdlet called Get-ExecutionPolicy.
Our result say’s ‘Restricted’. Hmmm, that’s not good. Let’s see how restricted we are by looking at all of the ways that it has been applied to our system. Each tier of the system hierarchy or Scope has its own execution policy status. We can choose which one we would like to set to unrestricted.
Let’s check by typing this command:
Bypass Execution Policy for a script file
There are many different ways to achieve a bypassed execution policy, but I personally use this method the most.
It is the easiest way to run a script in my opinion. All you need to do is add the following to your path when executing your script. Our example assumes that the script is named myscript.ps1
Powershell.exe -ExecutionPolicy Bypass -File myscript.ps1
And That’s It!
As we said earlier, there are tons of other ways to bypass the execution policy. You can pipe the output of your script to another PowerShell instance, you can encode and decode a script, you can execute a specific script block – there are so many different ways to do the same thing.
The method that we have highlighted allows you to run your script without running into any warnings or blockers, and is probably the most direct way to do it.
Hopefully you have found our post helpful, and that you can now run your scripts without running into this annoying blocker. If you would like to know how to check your PowerShell version in Windows 10 then head over to our blog post right over here.