powershellvisual-studio-code

How can I open Visual Studio Code through PowerShell and close PowerShell right after starting Visual Studio Code?


I would like to open Visual Studio Code for a specific folder. The command I try to run works, but when the script opens Visual Studio Code, it doesn't close the PowerShell window. I would like to close the PowerShell window right after the script opens the Visual Studio Code. But even an explicit exit call does not work:

ii F:\c#
iex "& code ."
exit

This opens Visual Studio Code, but the shell window stays open until I close Visual Studio Code.


Solution

  • This is the bug in the Visual Studio Code's backend. It unnecessarily attaches itself to the console window, preventing it from closing even after shell application quit.

    It is actually not related to PowerShell. You will get the same behavior with a .bat/.cmd file, which contains a single code command. If you double-click it in Windows Explorer, then console windows will not close until you close Visual Studio Code, even though in Task Manager you can see that the CMD instance used to execute the .bat/.cmd file no longer exists.

    To workaround this bug you can create new hidden console window for Visual Studio Code to attach to, instead of console window of your PowerShell instance:

    Start-Process -WindowStyle Hidden code .