CMD closes immediately on Windows 11

bugcommand lineviruswindows-11

For a couple of months, I could not open the command terminal. It closes immediately (I see only a frame for a short time). I'm on Windows 11. I see there are background processes that use cmd.

I can do this in Powershell:

PS D:\> cmd
Microsoft Windows [Version 10.0.22631.3447]
(c) Microsoft Corporation. All rights reserved.
PS D:\>

I found the cmd in the Windows folder, but it also does nothing. I even don't know what details I should share here.

I cannot use Visual Studio, because it calls cmd.

I found only this, which says it is probably a virus. I've avast and it found nothing. How can I remove it, if it is a virus?

Best Answer

The solution was to use regedit to delete the string value named autorun within the following Registry key:

HKCU\SOFTWARE\Microsoft\Command Processor
  • The autorun item contains one or more commands that are executed by CMD when it starts
  • This item was perhaps added when you installed some product, which was later perhaps uninstalled; you don't need to do anything beyond deleting it

See the article How-to: Autoexec commands for more information about autorun and similar alternatives:

To run a command as soon as the Command Prompt is opened, this can be useful for loading DOSKEY macros:

Add an entry in either:

  • Registry (Current user):
    HKCU\Software\Microsoft\Command Processor\AutoRun
    
  • Registry (All users):
    HKLM\Software\Microsoft\Command Processor\AutoRun
    

This will also run if a CMD.exe session is initiated by drag and drop onto a batch file or by a FOR /F command which calls a sub-process.

The AutoRun entry can contain a string value [REG_SZ or REG_EXPAND_SZ] which will be executed. These AutoRun keys are rarely used and don’t exist by default.

Things not to do if you set a Command Processor AutoRun:

  • which outputs any text: that is likely to mess up any scripts using the FOR /F command.
  • to launch CMD.exe: that will recursively launch new CMD sessions until the machine runs out of memory.
Related Question