Is not digitally signed. you cannot run this script on the current system. vscode

Run into the following bug trying to get TypeScript up and running in VSCode on your Windows machine?

The file tsc.ps1 is not digitally signed. You cannot run this script on the
current system. For more information about running scripts and setting execution policy, see
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.

Okay so here’s the deal. You probably installed the TypeScript compiler using the Windows Command Prompt or possibly the built-in PowerShell terminal from within VSCode. Installing TypeScript using the Command Prompt or the built-in terminal in VSCode will both cause your system to throw the nasty error above when you actually attempt to start using the TypeScript compiler.

Okay so… then what do we need to do? Well here’s your answer:

First, uninstall TypeScript.

$ npm uninstall -g typescript

Next, open up the Windows PowerShell (not the built-in PowerShell in VSCode) making sure to run the program as an administrator. If you don’t know how to do this, search for “PowerShell” in the search bar next to the Windows Start Button in the lower left and corner of your screen. Then right-click on “Windows PowerShell” and select “Run as administrator”. From the Windows PowerShell, you can now re-install TypeScript with administrator privileges which will fix the permissions related bug mentioned above.

$ npm install -g typescript

Now go ahead and re-open your TypeScript project in VSCode and compile it like normal, and everything should work A-okay 👌.

When you try to run a Powershell script (ps1), you may face error File cannot be loaded because running scripts is disabled on this system. This error may appear in Powershell, but also in ISE or Visual Studio Code (VS Code).

Is not digitally signed. you cannot run this script on the current system. vscode

This blog post explains:

  • why you get this error.
  • what your options are to keep Powershell safe.
  • how to fix it.

In a hurry? It’s not necessary to read the reason for this error. You may immediately skip to Fixes to File cannot be loaded because running scripts is disabled on this system

Cause

Powershell has built-in security features. One of them is the so-called Execution Policy. This policy can prevent scripts from running and can become useful in certain situations. For instance, in high-secure environments where running unknown Powershell scripts may be risky.  To view the current systemwide Execution Policy setting, type the following in Powershell:
Get-ExecutionPolicy

4 levels of security

Before you focus on fixing, it’s good to know the options. It may save you from using a risky or insecure setting. There are 4 policy levels to choose from. From most secure to most insecure:

  • Restricted: No Powershell scripts can be run. This is the default setting.
  • AllSigned: Scripts can be run, but all must have a digital signature. Even if you wrote the script yourself on the local computer.
  • RemoteSigned: Locally-written scripts can be run. But scripts from outside (email, IM, Internet) must be signed by a trusted publisher.
  • Unrestricted: Any script will run. Regardless of who created them or whether they are signed. 

In the fixes, I assume you have downloaded the script. Thus, I provide the Unrestricted option in each solution. But if you wrote the script yourself on your current pc, you’re better off using RemoteSigned.

There are several ways to fix the error “File cannot be loaded because running scripts is disabled on this system.” Each has its pros and cons. But whatever option you choose, it takes care of your problem anyway.
As I noted in 4 levels of security, Unrestricted is always a solution, but RemoteSigned is a more secure option.
All fixes are OS independent. They will work on Windows 10, Windows 2016, Windows 2019. And even on Windows 7.

Set ExecutionPolicy on computer (requires administrator privileges)

To set the ExecutionPolicy for all users on your computer:

    1. Open Powershell as Administrator
      Is not digitally signed. you cannot run this script on the current system. vscode
    2. Type Set-ExecutionPolicy Unrestricted and hit Enter.
      Is not digitally signed. you cannot run this script on the current system. vscode

    3. Confirm the Execution Policy Change by typing ‘Y‘ and hit Enter.
      Is not digitally signed. you cannot run this script on the current system. vscode
    4. Done! Everyone on the computer should now be able to run the script.
      Is not digitally signed. you cannot run this script on the current system. vscode

Note: once you have set the Execution Policy, it applies to all Powershell sessions in all applications. Do you use Visual Studio Code or Powershell ISE? The new setting also applies to those apps!

Set ExecutionPolicy on user

You don’t have Administrator privileges on your computer? You can still set the ExecutionPolicy to Unrestricted on the scope of the user. Here’s how:

  1. Open Powershell.
  2. Type Set-ExecutionPolicy Unrestricted -Scope CurrentUser and hit Enter.
  3. Confirm the Execution Policy Change by typing ‘Y‘ and hit Enter.
  4. Done! Your user account should now be able to run the script.

Run a Powershell session with ExecutionPolicy Bypass

To lower the security of Powershell for one time only, you can use the Bypass switch. This allows you to run a script temporarily while keeping the stricter settings for all other Powershell sessions.

  1. Open a command prompt.
  2. Type PowerShell -ExecutionPolicy Bypass and hit Enter.
    Powershell loads inside the cmd with lowered security.
  3. Navigate to the path of your script and run it.
  4. Once you close the Powershell window, the Bypass is closed with it.

Run the script in Powershell ISE (or Visual Studio Code)

This is not really running the script. Instead, I would call it the perfect work-around to bypass your strict (company’s) execution policy :)
In Powershell ISE, it’s possible to open a script. You are then able to view the code in the editor. Instead of running the ps1, do the following:

  1. select all code in the script.
  2. hit F8 to run the selected code.

This way you don’t need extra permissions to get your work done.

Is not digitally signed. you cannot run this script on the current system. vscode

View current ExecutionPolicy level

To view your current ExecutionPolicy, system-wide, type:
Get-ExecutionPolicy

But there are more ExecutionPolicy scopes to view. Type:
Get-ExecutionPolicy -list

This shows you:

  • MachinePolicy (defined by your system administrator).
  • UserPolicy (also defined by your system administrator)
  • Process (Bypass, when run as PowerShell -ExecutionPolicy Bypass)
  • CurrentUser (when Executionpolicy was set with -Scope CurrentUser)
  • LocalMachine (when Executionpolicy was set without extra arguments)

Back to 2008

In 2008, I wrote a similar blog post about the execution policy in Powershell. In Powershell v2, the error code was different:
File cannot be loaded because the execution of scripts is disabled on this system error
Powershell v3 and later had new capabilities. And changed the error to File cannot be loaded because running scripts is disabled on this system.

How do I run a PowerShell script that is not digitally signed?

Using an Unrestricted execution policy or temporary ByPass execution policy can fix the PowerShell script not digitally signed error. If you trust the downloaded script file from the internet, using the unblock-file cmdlet, unblock it and run it.

How do I make a PowerShell script digitally signed?

To create a self-signed certificate, use the New-SelfSignedCertificate cmdlet in the PKI module. This module is introduced in PowerShell 3.0 and is included in Windows 8 and Windows Server 2012. For more information, see the help topic for the New-SelfSignedCertificate cmdlet.

How do you solve running scripts is disabled on this system?

Solution for “cannot be loaded because running scripts is disabled on this system“:.
Open PowerShell Console by selecting “Run as Administrator” and set the execution Policy with the command: Set-ExecutionPolicy RemoteSigned..
Type “Y” when prompted to proceed..