86

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

Cerlancism
  • 2,803
  • 5
  • 14
  • 20
  • 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. – Scepticalist Nov 11 '19 at 08:24
  • I realised the older version of npm will not include the ps1 file, not sure why they started doing now. – Cerlancism Nov 11 '19 at 09:24
  • 2
    There is nothing wrong with setting the PowerShell script execution policy to at least "RemoteSigned". – Ansgar Wiechers Nov 11 '19 at 09:51
  • @Ansgar Wiechers alright then – Cerlancism Nov 11 '19 at 10:04

20 Answers20

184

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Troll
  • 1,895
  • 3
  • 15
  • 34
Esperento57
  • 16,521
  • 3
  • 39
  • 45
123

Use tsc.cmd instead of tsc. For example:

tsc.cmd -v
tsc.cmd --init
Troll
  • 1,895
  • 3
  • 15
  • 34
HungNM2
  • 3,101
  • 1
  • 30
  • 21
  • 4
    Just for clarification: `tsc` is a bash script - `tsc.cmd` is a Windows version of the script. – Mc Bton Nov 11 '22 at 17:36
27

Open Command_form : windows + R

Type : Powershell

Then type :

set-executionpolicy remotesigned

And select option : A

Sushil
  • 670
  • 7
  • 14
  • 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 '23 at 04:47
21

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

it will work

Ahmad Alhamad
  • 231
  • 2
  • 2
  • 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 '20 at 09:52
  • https://github.com/npm/cli/issues/470#issuecomment-586845035 npm in Windows will still install a cmd version – Cerlancism Jul 29 '20 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 '20 at 16:12
  • 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 '22 at 18:47
10

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

Troll
  • 1,895
  • 3
  • 15
  • 34
VISHNU M V
  • 111
  • 1
  • 3
7

Use tsc.cmd instead of tsc

example:

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

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

theProCoder
  • 390
  • 6
  • 21
Raghav Rathi
  • 187
  • 2
  • 7
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

Etemire Ewoma
  • 133
  • 1
  • 7
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
    Would like to insert your images directly in the text, or do prefer to leave them as links ? – XouDo May 17 '21 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

Amit Kumar
  • 21
  • 4
  • 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](/help/how-to-answer). – Community Apr 27 '23 at 08:58
2

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

tsc.cmd -v
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
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.

Troll
  • 1,895
  • 3
  • 15
  • 34
Enrique
  • 4,693
  • 5
  • 51
  • 71
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.
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
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

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

PV0208
  • 1
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

Marzieh Mousavi
  • 1,213
  • 1
  • 12
  • 28
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.

Nomnom
  • 4,193
  • 7
  • 27
  • 37
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.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31759877) – Sumit Sharma May 19 '22 at 04:49