-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
Alright — I’ll make you a final, polished “master script” that merges:
- Your mshta-based admin elevation
- My add/remove menu
- Your advanced detection & cleanup logic
- My earlier user-friendly prompts & 64-bit registry safety
And I’ll list exactly what’s upgraded from both previous scripts so you can raise a clean GitHub issue with a professional explanation.
📜 Final Merged Script — vscode_context_menu.cmd
@echo off
setlocal EnableDelayedExpansion
:: ========================================================
:: VS Code Context Menu Installer / Remover
:: Supports: Win7, Win10, Win11 (32/64-bit)
:: Features:
:: - Adds/Removes "Open with Code" for files, folders, backgrounds
:: - Auto-admin elevation (mshta) without PowerShell
:: - Detects VS Code from multiple install paths & registry
:: - Cleans old & duplicate context menu entries
:: - Direct registry writes (no temp files)
:: - Refreshes shell instantly (no explorer.exe kill)
:: ========================================================
:: --- Admin Elevation ---
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
)
:: --- Menu ---
echo.
echo ==========================
echo VS Code Context Menu Tool
echo ==========================
echo [1] Install "Open with Code"
echo [2] Remove "Open with Code"
echo [3] Exit
echo.
set /p choice="Enter choice: "
if "%choice%"=="3" exit /b
:: --- Detect VS Code Path ---
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
:: Registry fallback detection
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
)
:found
if not defined vscode_path (
if "%choice%"=="1" (
echo Error: VS Code not found in standard locations.
pause
exit /b 1
)
)
:: --- Function: Remove Entries ---
:remove_entries
echo Removing existing VS Code context menu entries...
for %%k in (
"HKCR\Directory\shell\OpenWithCode"
"HKCR\Directory\Background\shell\OpenWithCode"
"HKCR\*\shell\OpenWithCode"
"HKCR\Directory\shell\VSCode"
"HKCR\Directory\Background\shell\VSCode"
"HKCR\*\shell\VSCode"
) do reg delete %%k /f >nul 2>&1
goto :eof
:: --- Removal ---
if "%choice%"=="2" (
call :remove_entries
call :refresh_shell
echo.
echo "Open with Code" removed successfully.
pause
exit /b
)
:: --- Installation ---
if "%choice%"=="1" (
call :remove_entries
echo Installing "Open with Code" using: !vscode_path!
:: Folder menu
reg add "HKCR\Directory\shell\OpenWithCode" /reg:64 /ve /d "Open with Code" /f
reg add "HKCR\Directory\shell\OpenWithCode" /reg:64 /v Icon /d "\"!vscode_path!\",0" /f
reg add "HKCR\Directory\shell\OpenWithCode\command" /reg:64 /ve /d "\"!vscode_path!\" \"%%1\"" /f
:: Background menu
reg add "HKCR\Directory\Background\shell\OpenWithCode" /reg:64 /ve /d "Open with Code" /f
reg add "HKCR\Directory\Background\shell\OpenWithCode" /reg:64 /v Icon /d "\"!vscode_path!\",0" /f
reg add "HKCR\Directory\Background\shell\OpenWithCode\command" /reg:64 /ve /d "\"!vscode_path!\" \"%%V\"" /f
:: File menu
reg add "HKCR\*\shell\OpenWithCode" /reg:64 /ve /d "Open with Code" /f
reg add "HKCR\*\shell\OpenWithCode" /reg:64 /v Icon /d "\"!vscode_path!\",0" /f
reg add "HKCR\*\shell\OpenWithCode\command" /reg:64 /ve /d "\"!vscode_path!\" \"%%1\"" /f
call :refresh_shell
:: Verify
reg query "HKCR\*\shell\OpenWithCode" /reg:64 >nul 2>&1 && (
echo.
echo Success! "Open with Code" is now in your context menu.
) || (
echo.
echo Warning: Could not verify installation.
)
pause
exit /b
)
:: --- Shell Refresh ---
:refresh_shell
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
goto :eof💡 Key Upgrades from Before
Summary of Changes:
-
Unified Add/Remove Tool
- Combines installation and removal in a single menu-based script.
- No need for separate
.batfiles.
-
Cross-Version Admin Elevation
- Uses
mshtafor elevation, works on Windows 7–11 without PowerShell dependency.
- Uses
-
Robust VS Code Path Detection
- Checks
%ProgramFiles%,%LocalAppData%,%ProgramFiles(x86)%. - Falls back to VS Code’s registry uninstall entry.
- Checks
-
Duplicate Entry Cleanup
- Removes both
OpenWithCodeand defaultVSCoderegistry keys from older installs before adding new ones.
- Removes both
-
Direct 64-bit Registry Writes
- Uses
/reg:64to ensure Explorer (64-bit) can read the keys, avoiding WOW6432 redirection issues.
- Uses
-
No Temporary Files
- Direct
reg add/reg deletecalls — cleaner and faster.
- Direct
-
Non-Disruptive Shell Refresh
- Calls
SHChangeNotifyvia PowerShell and .NET to refresh menus instantly without killingexplorer.exe.
- Calls
-
Verification Step
- Confirms installation success by checking registry keys.
-
Self-Contained & No Cleanup Needed
- No temp files, no leftover processes.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels