0

Yesterday I experimented with OCaml and opam on my Windows system without using an installer—I just placed the binaries in a folder. A short time later, I removed the binaries. Now, whenever I run any batch file in PowerShell only, I get this:

C:\> .\test.bat
'opam' is not recognized as an internal or external command,
operable program or batch file.
"Hi!!"

Here is the contents of test.bat, but this is happening with any batch file I execute:

@echo off

echo "Hi!!"

As you can see from the output in PowerShell, it appears an attempt is made to execute opam, which, of course, cannot be found, since the binaries are removed. Then, the batch file runs normally. Note that this does not happen when running the batch file with cmd.exe.

What is going on here, and how can I fix it?

  • I cannot reproduce your problem, but the problem is most likely in your test.bat. You can try to remane it to test.cmd (yes that really does make a difference) and see if that works. Also, on what OS are you? Are you using Windows 10, or an earlier version? This is important because you may have an older version of PowerShell. – LPChip Dec 13 '17 at 18:28
  • Also, what happens if you add REM . as first line to the script? I sometimes see some weird bug where some random characters are added to the very start of a .cmd file. Usually this happens when someone makes the .bat file on a mac or linux system. You can verify this by running the batchfile from a commandline. You would see some weird characters then too. – LPChip Dec 13 '17 at 18:30
  • Any difference if you open Powershell via Win+r like this: Powershell -NoProfile? – root Dec 13 '17 at 18:57
  • @root No effect. – Jeff P. Dec 13 '17 at 21:51
3

Okay, I've found the answer:

There are a few little-known registry keys that cmd.exe looks for to run scripts or commands automatically on startup. The key in question was HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor\AutoRun. Apparently, when I ran opam, it placed opam configure --autorun in that registry key. Thus, whenever I started cmd.exe or ran a batch file in PowerShell (which invoked cmd.exe to run it), it attempted to run the command in that key. Since opam had been deleted (and removed from the system PATH), I was getting

'opam' is not recognized as an internal or external command,
operable program or batch file.

I found out about the existence of this registry key at Caelum's blog.

If you type cmd /?, the help text will list the registry keys that it looks for. The help text (on Windows 10) says:

If /D was NOT specified on the command line, then when CMD.EXE starts, it
looks for the following REG_SZ/REG_EXPAND_SZ registry variables, and if
either or both are present, they are executed first.

    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun

        and/or

    HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

So, to solve this, I looked for the registry key and deleted it. The problem now no longer occurs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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