56

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
  • 1,854
  • 3
  • 12
  • 18
  • 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
  • 1
    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

14 Answers14

134

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Troll
  • 1,978
  • 3
  • 13
  • 30
Esperento57
  • 15,129
  • 3
  • 36
  • 41
  • 11
    can you give some more details to what this does? and how safe it is? – PathToLife Mar 02 '20 at 17:13
  • 4
    It is super important to run the PowerShell as Administrator – Hakan Fıstık Mar 15 '20 at 18:10
  • 5
    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. – Hamza Khurshid Jul 07 '20 at 07:07
  • 1
    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7 – Esperento57 Jul 07 '20 at 11:30
  • 1
    Very good solution, solved my problem with typescript – Sergiu Mare Aug 23 '20 at 19:25
  • https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7 – Eran Or Oct 27 '20 at 15:59
  • Can add `-Scope CurrentUser` to avoid having to log in as admin – Elliott de Launay Oct 23 '21 at 16:37
  • or you can use command prompt terminal in vscode instead of the powershell (if you have the option) – Bradyo Nov 12 '21 at 02:20
  • The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at https://go.microsoft.com/fwlink/?LinkID=135170 . – Marzieh Mousavi Apr 17 '22 at 06:37
61

Use tsc.cmd instead of tsc. For example:

tsc.cmd -v
tsc.cmd --init
Troll
  • 1,978
  • 3
  • 13
  • 30
HungNM2
  • 2,050
  • 23
  • 19
17

Open Command_form : windows + R

Type : Powershell

Then type :

set-executionpolicy remotesigned

And select option : A

Sushil
  • 436
  • 5
  • 12
16

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

it will work

Ahmad Alhamad
  • 181
  • 1
  • 2
  • 4
    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
8

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

Troll
  • 1,978
  • 3
  • 13
  • 30
VISHNU M V
  • 91
  • 1
  • 2
4

Use tsc.cmd instead of tsc

example:

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

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

theProCoder
  • 272
  • 3
  • 16
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
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,978
  • 3
  • 13
  • 30
Enrique
  • 4,405
  • 5
  • 45
  • 66
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
  • 413
  • 1
  • 6
  • 17
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

-1

"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

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.

richardec
  • 14,202
  • 6
  • 23
  • 49
  • 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