Skip to content

Start Developer Command Prompt

Heath Stewart edited this page Nov 3, 2017 · 1 revision

Using vswhere you can easily find and start the Developer Command Prompt for Visual Studio.

Using Batch

@if not defined _echo echo off
for /f "usebackq delims=" %%i in (`vswhere.exe -prerelease -latest -property installationPath`) do (
  if exist "%%i\Common7\Tools\vsdevcmd.bat" (
    %comspec% /k "%%i\Common7\Tools\vsdevcmd.bat" %*
    exit /b
  )
)

rem Instance or command prompt not found
exit /b 2

If you prefer PowerShell, you can do start it after running vsdevcmd.bat to inherit environment variables.

@if not defined _echo echo off
for /f "usebackq delims=" %%i in (`vswhere.exe -prerelease -latest -property installationPath`) do (
  if exist "%%i\Common7\Tools\vsdevcmd.bat" (
    call "%%i\Common7\Tools\vsdevcmd.bat" %* && powershell.exe -nologo -noexit
    exit /b
  )
)

exit /b 2

In both cases, you can create a shortcut to a batch file with this content - e.g. vsdevcmd.cmd - and run it with a target like:

%comspec% /c "%userprofile%\bin\vsdevcmd.cmd"

Using PowerShell

If you prefer PowerShell and are already in a prompt and want to set environment variables without creating a nested prompt from which you have to exit twice, you can capture environment variables.

$installationPath = vswhere.exe -prerelease -latest -property installationPath
if ($installationPath -and (test-path "$installationPath\Common7\Tools\vsdevcmd.bat")) {
  & "${env:COMSPEC}" /s /c "`"$installationPath\Common7\Tools\vsdevcmd.bat`" -no_logo && set" | foreach-object {
    $name, $value = $_ -split '=', 2
    set-content env:\"$name" $value
  }
}

This script captures environment variables and sets (or overrides) them in your current process. You could put this into a script - .e.g. Start-VSDevCmd.ps1 - and expand on it to accept command line parameters matching what vsdevcmd.bat accepts, or even accept remaining arguments like Start-Process does to pass through.