Differences between commands run in .bat file and powershell.exe

I wanted to download scoop installer and I find out that my command is not working in a .bat file but works when I copy/paste it into PowerShell.

Here is the command and a picture to make things perfectly clear :

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

The output of PowerShell

enter image description here

Could somebody explain to me why it is the case and maybe the things that we need to be aware of when we put commands in .bat file ?

2 answers

  • answered 2018-01-11 20:45 Jeff Zeitlin

    Batch files are generally executed by CMD.EXE; Powershell is execute by PowerShell.exe. The two are different, and scripts that work in one will not work in the other. The code that you've pasted into your question is unquestionably PowerShell, not Batch. You might find it useful to look at some of Microsoft's PowerShell basics.

  • answered 2018-01-11 21:10 daddygames

    PowerShell share some commands with Windows Command Processor (cmd.exe). PowerShell includes extra features and commands that aren't in cmd.exe. Your command appears to be using PowerShell script.

    A .BAT file will execute, by default, using cmd.exe. You could change the file extension to .ps1, which should then default to using powershell.exe. Otherwise, you have to explicitly run the file against PowerShell.