93

On PowerShell, I got the error message when executing tsc. This never happened before.

I am not sure should I mingle with the PowerShell security settings to rectify this such as based on this one: PowerShell says "execution of scripts is disabled on this system."

Update

This is a new intended feature by npm to use ps1 scripts. A question has been raised in their repo: https://github.com/npm/cli/issues/470

4
  • You don't have much choice. The script execution policy needs to be set to allow running of .ps1 scripts on a Windows system. You can avoid by running within Powershell ISE or similar but otherwise the system policy will dictate. Nov 11, 2019 at 8:24
  • I realised the older version of npm will not include the ps1 file, not sure why they started doing now.
    – Cerlancism
    Nov 11, 2019 at 9:24
  • 2
    There is nothing wrong with setting the PowerShell script execution policy to at least "RemoteSigned". Nov 11, 2019 at 9:51
  • @Ansgar Wiechers alright then
    – Cerlancism
    Nov 11, 2019 at 10:04

20 Answers 20

192

You can run this in PowerShell command. Be sure to run it as administrator:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
11
  • 17
    can you give some more details to what this does? and how safe it is?
    – PathToLife
    Mar 2, 2020 at 17:13
  • 6
    It is super important to run the PowerShell as Administrator Mar 15, 2020 at 18:10
  • 15
    Please provide details on what it is and what it does. I see so many upvotes on an answer with no explanation. Please don't make SO a site where people come for hacks instead of learning. Jul 7, 2020 at 7:07
  • 2
  • 4
    Very good solution, solved my problem with typescript Aug 23, 2020 at 19:25
130

Use tsc.cmd instead of tsc. For example:

tsc.cmd -v
tsc.cmd --init
1
  • 4
    Just for clarification: tsc is a bash script - tsc.cmd is a Windows version of the script.
    – Mc Bton
    Nov 11, 2022 at 17:36
32

Open Command_form : windows + R

Type : Powershell

Then type :

set-executionpolicy remotesigned

And select option : A

1
  • Please try to run the windows power shell in admin mode And try this command, and this solution solved the same problem for me.
    – Gururaj
    Aug 10 at 4:47
21

this problem happend with me the solution is tsc.cmd [filename.ts]

it will work

4
  • 5
    This seems a bit unclear - question was regarding a powershell script. You're proposing to use tsc.cmd to run a typescript file - maybe expand a bit, and tell where to get the ts file and how i relates to tsc.ps1
    – Hauge
    Jul 29, 2020 at 9:52
  • github.com/npm/cli/issues/470#issuecomment-586845035 npm in Windows will still install a cmd version
    – Cerlancism
    Jul 29, 2020 at 11:58
  • 3
    I don't know why it's downvoted, but I found this useful, rather than changing execution policy in PowerShell.
    – N1gthm4r3
    Sep 22, 2020 at 16:12
  • 1
    This should be the accepted answer. The fix is on the npm developer side. They should name their ps script differently from the compiled executable.
    – Jeter-work
    May 13, 2022 at 18:47
11

If you are using Visual Studio Code you can use tsc.cmd instead of tsc. It worked fine for me.

1
  • this does it for me
    – user11720628
    Aug 14, 2021 at 21:17
8

Use tsc.cmd instead of tsc

example:

Before : tsc script.ts && node script.js

After : tsc.cmd script.ts && node script.js

2
  • Duplicate answer of stackoverflow.com/a/63150635/9319588
    – Cerlancism
    Feb 14, 2021 at 23:20
  • Downvoted too as the use of pipe operater | is erroneous, should use && instead if you want run the compiled script after transpiling in single line.
    – Cerlancism
    Feb 14, 2021 at 23:24
6

Solved

The error "tsc.ps1 cannot be loaded because running scripts is disabled on this system" occurs when the execution policy does not allow running the specific script on Windows.

To solve this issue you can use any of these three methods:


  1. Use the Set-ExecutionPolicy -ExecutionPolicy RemoteSigned command to solve the error.

Open your PowerShell as an administrator and set its execution policy with the Set-ExecutionPolicy command.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

If you aren't able to run the command as an administrator, try running it with the CurrentUser parameter.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

The Get-ExecutionPolicy command should display the execution policy for the current session (RemoteSigned). Use it to confirm if you have changed it.

Get-ExecutionPolicy
  1. Add .cmd to the command

By adding .cmd to the command it tells PowerShell to use the cmd version instead.

tsc.cmd -v

tsc.cmd --init

tsc.cmd myFile.ts
  1. You can also try prefixing the tsc command with npx

The npx prefix is an npm package runner that can run packages from the npm registry without installing them.

npx tsc -v

npx tsc --init

npx tsc myFile.ts

Read more from https://bobbyhadz.com/blog/typescript-tsc-cannot-be-loaded-because-running-scripts-disabled

2

If your using Powershell as default shell then you will get this error on Running: as shown

To fix this you can have following option. you can choose any of these to short out your issue:

  1. you have to enable execution policy by following command in PS(Powershell):

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

once its enable you able to run tsc command directly.

  • Since Powershell is default terminal shell you use following command to run all typescript related commands like :

    tsc.cmd -v

  • You can change you default shell from Powershell to cmd by following method in Visual studio code. then using cmd shell you can use typescript normally like tsc -v instead of using tsc.cmd -v

steps to enable cmd.exe as default shell for VS Code:

  • Press ctrl+shift+p

  • Press 'terminal: select default profile' as shown

  • Select cmd.exe as default terminal shell. as shown

  • Now in terminal window you will see cmd.exe as shown

1
  • 1
    Would like to insert your images directly in the text, or do prefer to leave them as links ?
    – XouDo
    May 17, 2021 at 10:35
2
  1. Open your PowerShell as an administrator and set its execution policy with the "get-ExecutionPolicy" command. it will show you restricted.

  2. type "Set-ExecutionPolicy Unrestricted"

  3. ask you yes for all type [A]

Other Option

  1. Type "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned"

  2. check tsc --int

  3. See below steps

enter image description here

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 27 at 8:58
2

In VS Code, use tsc.cmd instead of tsc. For example:

tsc.cmd -v
1

You can also go to File > Settings > Extensions > JSON > Schema and add this to that JSON:

"terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"]

Restart and it should work.

1

I had a problem like this. I have solved my problem with doing this:

  1. Try this: open terminal/cmd and type: npm install typescript --save-dev,
  2. compile with: npx tsc or you can do this: npx tsc --watch. More about that: https://www.typescriptlang.org/download.
0

"File Cannot Be Loaded Because Running Scripts Is Disabled on This System In Windows Powershell FIX." This is observed in latest update in permissions by windows all we need to do is search for Powershell in windows start and run as administartor and runn the followiing commands which is given below.

If you are running into an error saying that your script "cannot be loaded because running scripts is disabled on this system" while trying to run a script in Powershell, on Windows 10, you have to change the execution policies first.

Commands to be entered in power shell:

1)Get-ExecutionPolicy -List

2)Set-ExecutionPolicy Unrestricted

3)Set-ExecutionPolicy Unrestricted -Force

Note:- u will be prompted to give Y/N after giving the 2nd command,so give as 'Y' and click on enter button

If PowerShell throws up an error message – File cannot be loaded because running scripts is disabled on this system, then you need to enable script running on your Windows 10 computer. The cause of this error comes to the fact that your user account does not have enough permissions to execute that script. This does not mean that you need to have an Administrator level permissions, it also means that you also need to be unrestricted to run these type of PowerShell scripts or cmdlets

1
  • No, this is a workaround, and a dangerous one at that. This should be deleted.
    – Jeter-work
    May 13, 2022 at 18:46
0

In visual studio code-> terminal ->open new terminal Then in right corner of terminal there is an arrow ↓ besides + sign -> select "cmd" Then you can run tsc filename.ts command without using tsc. cmd filename.ts Then use node filename Here is your output... taaaa-daaaaa

0

in VS code it will be solve by adding this code to the setting.json.

"terminal.integrated.profiles.windows": {
  "PowerShell": {
    "source": "PowerShell",
    "icon": "terminal-powershell",
    "args": ["-ExecutionPolicy", "Bypass"]
  }
},
"terminal.integrated.defaultProfile.windows": "PowerShell",

refrence

0

if you do just this

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

you might be greeted with the error

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for
the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change the execution policy for the current user, run "Set-ExecutionPolicy
-Scope CurrentUser". At line:1 char:1

  • Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
  •   + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
    
    

So, what you have to do is run windows powershell as administrator then type this:

Set-ExecutionPolicy -Scope CurrentUser

Then set,

ExecutionPolicy: remotesigned

Then say yes to all by typing A and everything will be okay. goodlu

0

I strongly recommend to install the new open-source powershell if you're on windows https://github.com/PowerShell/PowerShell/releases

This fixed all our woes, including running scripts being disabled. We also had issues with git bash regarding the way paths are handled differently (ex: backslash, forward slash).

It's the simplest and most foolproof solution in my opinion, I hope it helps.

0

Below command works for me. Run in Shell Terminal as Administrator:

For more information check the detailed blog by ms

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Or

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

enter image description here

-1

Another solution for the above problem is if you are using VScode or Visual Studio Code.

  1. Open the vscode termial by using Ctrl + ` or go on the terminal tab and open it.
  2. You can see there are a few tabs and icons.
  3. At the top right there would be a symbol 'v' beside the + sign from there select cmd and you are good to go.
  4. Example: tsc file.ts (here file is the name of the file you want to compile)
  5. As soon as you execute the above command you will get a javascript file, in this case, file.js will be generated...

Hope your issue would be solved...happy coding

-1

Go to C:\Users\user_name\AppData\Roaming\npm and remove tsc.ps1.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.