Can’t Open CMD.exe After Uninstalling Python/Anaconda; Exit Code 1

Symptoms

After uninstalling Python/Anaconda on your Windows computer, Command Prompt may not open. When you run cmd.exe, it flashes on the screen and immediately quits. Also, running DISM and SFC (to repair corrupt system files), repairing Windows installation, and running a thorough malware scan may not fix the issue.

Also, when you open Command Prompt via the Terminal menu in Visual Studio Code, the following error (Error 1: “Incorrect function”) occurs:

The terminal process "C:\WINDOWS\System32\cmd.exe" terminated with exit code: 1.

vscode cmd.exe error 1

If you had enabled conda init powershell and later uninstalled Anaconda, PowerShell might report the following error at every start:

The term 'C:\ProgramData\Anaconda3\Scripts\conda.exe' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At C:\Users\Ramesh\Documents\WindowsPowerShell\profile.ps1:3 char:4

The profile.ps1 file would have the following code in it, which auto-starts (PowerShell hook) whenever you launch PowerShell:

#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
(& "C:\ProgramData\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression
#endregion

Cause

This issue occurs due to an incorrect and leftover Autorun registry value after uninstalling Python.

autorun command processor python

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command ProcessorAutorun REG_SZ "if exist"

The value data might read “if exist” which CMD.exe executes at launch and closes automatically. The registry value was added by your Python/Anaconda installation. However, you should be able to launch Command Prompt correctly by running “cmd.exe /d“.

/D  – Disable execution of AutoRun commands from the registry (see below)



  • 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

The PowerShell error is caused by the PowerShell script file profile.ps1, which was placed by the “conda init powershell” command you ran earlier.

The script file would be located at "%Userprofile%\Documents\WindowsPowerShell\profile.ps1"

Resolution

To resolve the Command Prompt issue, delete the Autorun registry key. The Autorun value doesn’t exist by default in Windows and can be deleted.

  1. Open an admin Command Prompt window. To do so, type cmd.exe /d in Start/Run dialog, and press Ctrl + Shift + Enter. (Ref: Start Programs Elevated from Run Dialog)
  2. In the admin Command Prompt, type the following commands:
    reg.exe delete "HKCU\SOFTWARE\Microsoft\Command Processor" /v "Autorun" /f

    and then:

    reg.exe delete "HKLM\SOFTWARE\Microsoft\Command Processor" /v "Autorun" /f

    In case you receive the following error, please ignore it:

    ERROR: The system was unable to find the specified registry key or value.
  3. Exit the Command Prompt window.

You should be able to launch cmd.exe (with or without the /d parameter) now.

To resolve the PowerShell startup error, delete the “profile.ps1” file located under your “\Documents\WindowsPowerShell\” directory.


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support. It won't take more than 10 seconds of your time. The share buttons are right below. :)

Ramesh Srinivasan is passionate about Microsoft technologies and he has been a consecutive ten-time recipient of the Microsoft Most Valuable Professional award in the Windows Shell/Desktop Experience category, from 2003 to 2012. He loves to troubleshoot and write about Windows. Ramesh founded Winhelponline.com in 2005.

Leave a Comment