Search code examples
powershellvisual-studio-codeshell-extensions

Powershell equivalent of "Open with Code" Windows shell extension?


When I choose the "Open with Code" shell extension in Windows Explorer, it opens the file in an existing VS Code window. I'd like to do the same from a Powershell script in my build process to load some output logs into VS Code.

Just executing:

& "C:\Program Files\Microsoft VS Code\Code.exe" .\install.log

always opens a new instance

I have looked at the registry change here but it does not behave the way the shell extension behaves - it always just opens a new Code instance. Also, despite additional comments here about making new registry entries, that is not actually how the shell extension works.

Is this a case of finding the existing running Code process and sending it a message to open the file?

Unfortunately, using:

Get-Process | Where-Object { $_.ProcessName -eq "Code" }

with one Code window open, shows eight Code processes...

Or perhaps the existing shell extension code is already available somewhere so I can see what it does to get existing Code instance to open a file?

Issue is because VS Code has changed installation locations over time

Issue is because VS Code has two installations

Resolution: As long as you are actually calling the same binary as the existing vscode processes, it will open in an existing window (based on current operation). So important to verify that you don't have multiple binaries due to installation path changes by vendor over time. This also affects the registry fixes mentioned in the links above as well.


Solution

  • Actually this...

    [Get-Process | Where-Object { $_.ProcessName -eq "Code" }] 
    

    ...even to what looks like that you have one Code.exe window open actually it will always be 2 or more, depending on what and how you have VSCode configured. For example, doing this on my dev box brings back 9...

    Get-Process | 
    Where-Object { $_.ProcessName -eq "Code" } | 
    Select-Object -Property ID, SessionId, Path, PriorityClass
    
    <#
       Id SessionId Path                                                                 PriorityClass
       -- --------- ----                                                                 -------------
     1200         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
     2520         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
     5168         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
     6292         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe   AboveNormal
    10856         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
    13556         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
    16920         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
    19528         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
    20424         2 C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code\Code.exe        Normal
    #>
    

    ...and this is on launch.

    Also, why are you calling VSCode this way?

    & "C:\Program Files\Microsoft VS Code\Code.exe" .\install.log
    

    As on install, it gets added to your user path (unless you changed that on install)...

    C:\Users\postanote\AppData\Local\Programs\Microsoft VS Code
    

    ... thus you only need to do this...

    code D:\Temp\abc.txt
    

    ... when calling it from the PowerShell consolehost, ISE, VScode, et al, and it will load in the current instance you are working in.

    I do this daily. Before the advent of VSCode, in the ISE, you call psEdit to do the same.

    psEdit -filenames D:\Temp\abc.txt
    

    If you are already in VScode, just hit F1, then backspace, then type in the full path to the file. No other code startup required.