Change Default Visual Studio Command Prompt Location - visual-studio

How to set the default visual studio command prompt location so that I can go straight to my project's directory instead of using the usual navigation.

For Visual Studio 2017 Command Prompt, you need to set environment variable VSCMD_START_DIR to the directory where you want to end up after the command prompt initializes.
I use this script:
set VSCMD_START_DIR=%1
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat"
It accepts the directory as the first (and only) argument.

Add it as an external tool to Visual Studio (2008 shown here, should be similar in other versions):
Select "Tools", "External Tools...".
Click Add
Title: &Cmd
Command: cmd.exe
Arguments: /k "c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
Initial Directory: $(SolutionDir)
Note the arguments come from the "Visual Studio 2008 Command Prompt" shortcut. Yours may vary.
You can can customize the toolbar and add a button for this command as well using "Tools", "Customize...". Open the Tools menu and locate the last External Tool you created and drag it onto the toolbar.
When you click the button, it will open a command prompt in the current solution's root directory.

In Visual Studio 2013,
Right click on "Developer Command Prompt for VS2013" shortcut (which you can find in the "Common7\Tools\Shortcuts" folder from where you installed your VS2013) and choose "Properties",
Change the: "Start in: " directory to your desired location.

Navigate to the "vcvarsall.bat" file. For me the path is:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat
Add this line to the bottom of the script:
cd /d "D:\WhereverYouWant"
skol

For Developer Command Prompt for VS 2019 you can set VSCMD_START_DIR environment variable with the following command:
setx VSCMD_START_DIR C:\your-folder

You can put these lines in a batch script (vcvar.bat) located in the directory you want to start with:
#echo off
set VCDIR=%ProgramFiles%\Microsoft Visual Studio 10.0\VC
if not exist "%VCDIR%" set VCDIR=%ProgramFiles(x86)%\Microsoft Visual Studio 10.0\VC
call "%VCDIR%\bin\vcvars32.bat"
Fire a command prompt in this directory, and call vcvar.bat. You now have the VS environment in the command prompt.

Copy Windows Command Processor from "C:\Windows\System32\cmd.exe" and paste to "C:\Your\Project\Directory\cmd.exe"
Edit Visual Studio Command Prompt shortcut by replacing %comspec% with "C:\Your\Project\Directory\cmd.exe" in the Target field.
So the shortcut's Target should look something like: "C:\Your\Project\Directory\cmd.exe" /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" amd64

this will add a menu entry "my compiler " to each directory giving you access to the command prompt in the directory.
save it as something.reg
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\My Compiler]
[HKEY_CLASSES_ROOT\Directory\shell\My Compiler\command]
#="cmd.exe /k \"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\vcvarsall.bat\""

Related

How to run devenv.exe command line switches (for Visual Studio 2019) from git bash

My primary use case is I'd like to use devenv.exe /edit command to open files in an already running instance of Visual Studio 2019, using git bash.
The issue I'm having is that I cannot seem to finesse git bash to run the command in a way that actually works.
Specifically, this is what I'm seeing:
from windows cmd:
command: devenv /edit foobar.c
results: SUCCESS!; opens foobar.c in an existing instance of visual studio 2019
from git bash:
command: devenv /edit foobar.c
result: FAIL!; opens a new instance of 2019, and has an error popup saying: The following files were specified on the command line: "C:/Program Files/Git/edit" These files could not be found and will not be loaded. Opens file
from git bash, tucking the command under a .bat file
mybat.bat contents: devenv /edit foobar.c
command: cmd //c mybat.bat
result: FAIL!; Opens a new instance of 2019, no error popup, opens file.
from git bash, using full paths to exe and file
command: "/c/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/devenv.exe" /edit /c/Users/MYNAME/foobar.c
result: FAIL!; opens a new instance of 2019, and has an error popup saying: The following files were specified on the command line: "C:/Program Files/Git/edit" These files could not be found and will not be loaded. Opens file
from git bash, escaping the edit command and and using full paths:
command: "/c/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/devenv.exe" /edit /c/Users/MYNAME/foobar.c
result: FAIL!; Opens a new instance of 2019, no error popup, opens file.
from git bash, after opening the sln via git bash:
command 1: devenv.exe my_solution.sln &
result: Opens my solution
command 2: devenv.exe //edit foobar.c &
result: SUCCESS!; opens foobar.c in an existing instance of visual studio 2019
Command 3 (from a different git bash): devenv //edit foobar.c &
result: FAIL!; Opens a new instance of 2019, no error popup, opens file.
Can anyone think of a method to invoke the command from git bash, in the same fashion that a windows command prompt (cmd.exe) would invoke the command?
The only solution I could find was to put "& "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe" "solution.sln" /Build Release in a .ps1 file and call powershell build.ps1 from my .sh file.

How to execute a shortcut with space in the target field from the command line?

For example, I am trying to execute a Dynamics GP shortcut icon from the command line.
Target is as follows:
"C:\Program Files (x86)\Microsoft Dynamics\GP2018\Dynamics.exe" Dynamics.set
What is the command to execute the following shortcut through Windows command line without double clicking on the shortcut icon in order to open it?
"C:\Program Files (x86)\Microsoft Dynamics\GP2015\Dynamics.exe" "C:\Program Files (x86)\Microsoft Dynamics\GP2015\Dynamics.set"
It took me a few tries to do it, but that worked

VS 2015 Command Prompt Error:"the filename,directory name, or volume label syntax is incorrect"

Added a command prompt to VS 2015 with these instructions:
Open Tools -> “External Tools…”
Click “Add”
Enter the following information into the new tool screen:
Field Value
Title VS Command Prompt
Command: C:\Windows\System32\cmd.exe
Arguments: /k “C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat”
Initial Directory: $(SolutionDir)
Save your changes (and re-order your tools to suit your preferences). The “VS Command Prompt” will now be available in:
Tools -> VS Command Prompt
Every time the prompt is opened, it shows the error "the filename,directory name, or volume label syntax is incorrect".
Is there anything else I can do in VS or add a path to the environment variables?
The problem is in the argument
/k “C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat”
It should be
/k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"
I encountered the same problem when I copied the argument from the website
https://www.stevefenton.co.uk/2015/06/add-visual-studio-command-prompt-to-visual-studio-2015/

Change directory in visual studio command prompt

I want to know what is the command I should execute in the command window of the visual studio 2013, to change directory to:
C/:Users/user/Documents/Visual Studio 2013/Projects/OutlookAddin/publish/<Most_Recent_Version>/OutlookAddin_TemporaryKey.pfx
What is the command I must execute?
Actually, cd cmd works, but in a bit diff like this:
syntax:
cd /d c: or e: etc..
where /d stands for drive
for instance:
cd /d C/:Users/user/Documents/Visual Studio 2013/Projects/OutlookAddin/publish//OutlookAddin_TemporaryKey.pfx
No need for the single or double quote in this case.
Now to execute any type of files for instance simply add '.\' before the executed one.
syntax:
.\OutlookAddin_TemporaryKey.pfx or .\example.exe what so ever
cd does work. However, keep in mind that if the path you're trying to get to has spaces, you need quotes around it (the path). E.g.:
cd "C:\Program Files\Microsoft Visual Studio 10.0" Also, note that the
"Visual Studio command prompt" that is mentioned in Step 6 is not the
"Visual Studio Command Window" - I think this is where the problem
comes from. The command prompt is a separate executable that you can
start by clicking Start, point to All Programs, point to Microsoft
Visual Studio, point to Visual Studio Tools, and then click Visual
Studio Command Prompt. Alternatively, just search "Visual Studio" in
the search bar in the Start menu, and it should show up
Source: Using the Command Window in Visual Studio
First create a file on CMD (COMMAND PROMPT)
1.cd desktop
2.mkdir filename
3.cd filename
4. code .
(It has to be a space between code and the dot to open up the file in visual studio code)
Note: If you just want to open visual studio, then go to CMD and just type in 'code' without the dot.

Use PowerShell for Visual Studio Command Prompt

In a serious intiative to migrate all my command line operations to PowerShell, I would like to avoid using the old fashioned command console for anything. However, the Visual Studio Command prompt has various environment variables and path settings not found in the default command prompt. How could I create a 'Visual Studio PowerShell' with those same settings?
You can use for example this script to import Visual Studio command prompt environment, see the examples in the script documentation comments, e.g. for Visual Studio 2010:
Invoke-Environment '"%VS100COMNTOOLS%\vsvars32.bat"'
Having done that in the beginning of a PowerShell session (from your profile or manually), you get what you ask for in this PowerShell session.
Or you can use the solution provided by Keith Hill in this answer.
have a look at PowerConsole
PowerConsole has been incorporated into NuGet http://nuget.codeplex.com/. You get PowerShell inside Visual Studio and a package management system.
I use this script that I call Initialize-VisualStudio.ps1, i call it in my profile with dot source, to set the environment variables need it, in my actual session:
param([switch]$ArquitectureX86)
if($ArquitectureX86)
{ $arq= "x86"}
else
{ $arq="x64"}
pushd 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC'
cmd /c "vcvarsall.bat $arq&set" |
foreach {
if ($_ -match "=") {
$v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])";
}
}
popd
What I do is create a simple cmd batch command script that looks like this:
call "%VS80COMNTOOLS%vsvars32.bat"
powershell
Then I create a shortcut that invokes this through cmd. The shortcut target looks like:
%windir%\System32\cmd.exe /k "SetupPSBuildEnvironment.cmd"
If you want the console to look like the powershell console, just modify the Layout to your liking in the shortcut properties.
First, check the contents of this folder:
C:/ProgramData/Microsoft/VisualStudio/Packages/_Instances/
There'll be another folder in it with a name consisting of hex digits (e.g. 2a7a9ed6, but that will vary for different MSVC versions). I'll refer to it as <instance_id>.
Then run from PS:
Import-Module 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell <instance_id> -DevCmdArguments '-arch=x64'
Or you can create a shortcut with the following target:
<path to your powershell.exe> -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell <instance_id> -DevCmdArguments '-arch=x64'}"
Obviously, drop -arch=x64 if you need x86 toolset.
Works for me on Windows 10 with MS Build Tools 16.9.5 and PowerShell 5.1.19041,7.1.3

Resources