Skip to content

Upgrade Suggestion #2

@DK26

Description

@DK26

May I offer an upgrade?

VS Code Context Menu Script - Key Features

  • Refreshes shell immediately without killing explorer.exe
  • Double-click installation with automatic administrator privilege elevation (using mshta instead of PowerShell)
  • Searches multiple VS Code installation paths including registry fallback
  • Removes duplicate context menu entries from previous installations
  • Direct registry modification without temporary files
  • Adds context menus to files, folders, and folder backgrounds
  • Compatible with Windows 7, 10, and 11
  • Self-contained with no external dependencies or cleanup needed
  • Includes installation verification and clear status messages

File: restore_vscode_context.cmd

@echo off
setlocal enabledelayedexpansion

:: Optimized batch script to add "Open with Code" to context menu

:: Check admin rights and elevate if needed
net session >nul 2>&1 || (
    echo Requesting administrator privileges...
    mshta "javascript:var sh=new ActiveXObject('Shell.Application'); sh.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
    exit /b
)

:: Detect VS Code installation
set "vscode_path="
for %%p in (
    "%ProgramFiles%\Microsoft VS Code\Code.exe"
    "%LocalAppData%\Programs\Microsoft VS Code\Code.exe"
    "%ProgramFiles(x86)%\Microsoft VS Code\Code.exe"
) do if exist "%%~p" set "vscode_path=%%~p" & goto :found

:: Fallback: Check registry for installation path
for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1" /v InstallLocation 2^>nul') do (
    if exist "%%b\Code.exe" set "vscode_path=%%b\Code.exe" & goto :found
)

echo Error: VS Code installation not found.
pause & exit /b 1

:found
echo Found VS Code: !vscode_path!

:: Remove existing entries first (including VS Code installer entries)
echo Removing existing context menu entries...
reg delete "HKCR\Directory\shell\OpenWithCode" /f >nul 2>&1
reg delete "HKCR\Directory\Background\shell\OpenWithCode" /f >nul 2>&1
reg delete "HKCR\*\shell\OpenWithCode" /f >nul 2>&1
reg delete "HKCR\Directory\shell\VSCode" /f >nul 2>&1
reg delete "HKCR\Directory\Background\shell\VSCode" /f >nul 2>&1
reg delete "HKCR\*\shell\VSCode" /f >nul 2>&1

:: Add folder context menu
echo Adding folder context menu...
reg add "HKCR\Directory\shell\OpenWithCode" /ve /d "Open with Code" /f || goto :error
reg add "HKCR\Directory\shell\OpenWithCode" /v Icon /d "\"!vscode_path!\",0" /f || goto :error
reg add "HKCR\Directory\shell\OpenWithCode\command" /ve /d "\"!vscode_path!\" \"%%1\"" /f || goto :error

:: Add background context menu
echo Adding background context menu...
reg add "HKCR\Directory\Background\shell\OpenWithCode" /ve /d "Open with Code" /f || goto :error
reg add "HKCR\Directory\Background\shell\OpenWithCode" /v Icon /d "\"!vscode_path!\",0" /f || goto :error
reg add "HKCR\Directory\Background\shell\OpenWithCode\command" /ve /d "\"!vscode_path!\" \"%%V\"" /f || goto :error

:: Add file context menu
echo Adding file context menu...
reg add "HKCR\*\shell\OpenWithCode" /ve /d "Open with Code" /f || goto :error
reg add "HKCR\*\shell\OpenWithCode" /v Icon /d "\"!vscode_path!\",0" /f || goto :error
reg add "HKCR\*\shell\OpenWithCode\command" /ve /d "\"!vscode_path!\" \"%%1\"" /f || goto :error

:: Refresh shell without restarting Explorer
echo Refreshing shell...
powershell -NoProfile -Command "& {Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class Shell { [DllImport(\"shell32.dll\")] public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); }'; [Shell]::SHChangeNotify(0x08000000, 0x1000, [IntPtr]::Zero, [IntPtr]::Zero)}" >nul 2>&1

:: Verify installation
reg query "HKCR\*\shell\OpenWithCode" >nul 2>&1 && (
    echo.
    echo Success! "Open with Code" added to context menu.
    goto :end
) || (
    echo Warning: Verification failed.
    goto :end
)

:error
echo.
echo Error: Failed to modify registry.
pause & exit /b 1

:end
echo.
pause

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions