11

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.

5
  • start -WindowStyle Hidden code . Commented Aug 3, 2019 at 6:32
  • this is great, even tho its still open in the background, but it dont bothers me, because its "invisible". Thanks a lot. Commented Aug 3, 2019 at 6:47
  • Looks like it is related to this bug. Commented Aug 3, 2019 at 7:01
  • seems like they have fixed the bug. I just tried to close powershell after opening vscode using code . and ONLY powershell closed :D Commented Mar 27, 2021 at 11:22
  • What are "ii" and "F:\c#"? Commented Jun 1 at 22:55

8 Answers 8

18

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 .
Sign up to request clarification or add additional context in comments.

3 Comments

The OP doesn't seems to bother about the cmd window I would like to close the PowerShell window...
@Moerwald Are "cmd window" and "PowerShell window" different things? A far as I know them both use the same console window, provided by operation system.
Luckily this bug has been fixed now, it no longer attaches to the console
17

To open your project by using PowerShell,

  1. Go to your project directory after pressing Shift + right-click
  2. Select "Open PowerShell window here"
  3. Just type code . and press Enter.

Comments

4

Example:

function code {
    $code_path = "${Env:LOCALAPPDATA}\Programs\Microsoft VS Code\bin\code.cmd" -replace ' ', '` '
  "${code_path} $args" | Invoke-Expression
}

Comments

0

I created helper functions in my PowerShell $profile called idea and vscode:

    function idea {
        Start-Process -FilePath "D:\Program Files\JetBrains\IntelliJ IDEA 2021.2.1\bin\idea64.exe" -ArgumentList "."
        exit
    }

function vsc {
    Start-Process -FilePath "D:\Users\AppData\Local\Programs\Microsoft VS Code\Code.exe" -ArgumentList "."
    exit
}

Comments

0

In case you only have Visual Studio Code Insiders installed and not Visual Studio Code installed, then code won't be a recognized command in PowerShell.

If this is the case, then you can use this command:

code-insiders

If you would like to use Visual Studio Code instead of Visual Studio Code Insiders, but don't want to install the non-insiders version, then add this to your $PROFILE by opening the configuration file...

code-insiders $PROFILE

...and copy/paste this somewhere inside the file:

Set-Alias code code-insiders

Then relaunch your terminal.

Comments

0

There is a workaround by starting Visual Studio Code from a runspace instead.

$Powershell = [PowerShell]::Create()
$null = $Powershell.AddScript({code})
$Handle = $Powershell.BeginInvoke()
$Powershell.EndInvoke($Handle)

Comments

-1

This works for me:

iex "code ."
$host.SetShouldExit(0)

Comments

-1

Your problem is that calling exit will exit the script and not the PowerShell instance you called it from. You can define a function in a script file (or in your profile.ps1). Afterwards you source the script file via:

. .\StartCode.ps1

Content of StartCode.ps1:

function Start-CodeInViaStartProcess {
    Start-Process -FilePath "code" -ArgumentList "."
    exit
}

Since the content of StartCode.ps1 is now sourced to your PowerShell session exit will terminate the actuall PowerShell session. Therefore Start-CodeInViaStartProcess should terminate your PowerShell window.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.