Search code examples
powershellunity-game-enginebatch-filecmd

cmd.exe throws error "& was unexpected at this time."


so my problem is that when i just ran cmd.exe in terminal, i get "& was unexpected at this time." Error at the end - looks like this

enter image description here

So the problem is that i'm getting erros in Unity 3D when it wants to run the unity_csc.bat file and compile solution. These errors are exactly the same as the one when i just run cmd.exe - therefore i suspect its not an Unity3D based problem (if you want you can check the Unity3D specific thread here https://forum.unity.com/threads/2-empty-errors-in-console-was-unexpected-at-this-time.799110/ )

Does anyone know why this might be happening ? This also happens when i try to run a .bat file - which I suspect is why i cant compile Unity project

enter image description here

I'm running Windows 10 with all of the latest updates

EDIT:

Since cmd /d does not throw the error, might there be some problem with this registry record ? enter image description here

In User Folder i do have this Autorun record

@mode 20,5 & tasklist /FI "IMAGENAME eq SoundMixer.exe" 2>NUL | find /I /N "SoundMixer.exe">NUL && exit & if exist " ( start /MIN "" " & tasklist /FI "IMAGENAME eq explorer.exe" 2>NUL | find /I /N "explorer.exe">NUL && exit & explorer.exe & exit ) else ( tasklist /FI "IMAGENAME eq explorer.exe" 2>NUL | find /I /N "explorer.exe">NUL && exit & explorer.exe & exit )

enter image description here


Solution

  • Stephan has provided the crucial pointer:

    It sounds like you have a broken autorun command defined for cmd.exe; that is, your registry defines a command that is automatically executed whenever you call cmd.exe, and that command causes the syntax error you're seeing.

    Note that such commands are executed irrespective of whether you open an interactive cmd session or invoke via a batch file or pass a command to cmd with /C.

    Passing /D to cmd bypasses any autorun commands.

    There are two locations in the registry where such a command can be defined, one at the local-machine level (which applies to all users),
    HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor, and another for the current user only,
    HKEY_CURRENT_USER\Software\Microsoft\Command Processor, in a value named AutoRun.
    If commands are defined in both locations, the HKEY_LOCAL_MACHINE's commands run first.

    To list any defined autorun commands:

    Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun
    

    You can use the following PowerShell snippet to remove autorun commands from both locations, but note that you'll have to run it with elevation (as administrator), if a local-machine value is present:

    Get-ItemProperty -ea Ignore ('HKCU:', 'HKLM:' -replace '$', '\Software\Microsoft\Command Processor') AutoRun |
      Remove-ItemProperty -Name AutoRun -WhatIf
    

    Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf and re-execute once you're sure the operation will do what you want.
    Also note the unfortunate need to repeat the value name (AutoRun) in the Remove-ItemProperty call, because only the item path (registry key path), not also the property name (registry value name) can be bound via the pipeline.