How can I open a solution file from powershell? - visual-studio

I'm new to powershell and I'm looking to create a powershell script to open two different solution files with two different versions of visual studio. I have the commands for opening up the different visual studio versions, but not sure how to open the solutions with each one. Here is what I have so far.
$vs19 = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\devenv.exe"
$vs19WorkDir = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\"
$vs15 = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe"
$vs15WorkDir = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\"
Start-Process $vs19 -WorkingDirectory $vs19WorkDir
Start-Process $vs15 -WorkingDirectory $vs15WorkDir

Although not super obvious at first glance, the devenv command line reference mentions:
When specifying a solution or project, the first argument is the name of the solution file or project file, including file path.
With this in mind, I tried the following against Visual Studio 16 (2019):
& "path\to\devenv.exe" "path\to\a\solution.sln"
and it opened the solution in question, so you should be able to do it with:
Start-Process $vs19 -WorkingDirectory $vs19WorkDir -ArgumentList "c:\path\to\solution.sln"

I wrote a Powershell script to solve this problem. It was inspired by #Mathias R. Jessen's answer. You can find it on this Github gist
EDIT:
I shared the gist on Reddit as well and it turns out that there are much cooler and intuitive ways of achieving this on PowerShell. Check them out as well.
PS: `The fastest method I've seen so far is by typing *.sln, then hitting Tab and Enter

Related

Visual studio: How to open a specific solution directly from command line?

I want to open a specific solution (.sln) in visual studio directly from the command line.
Using the command, I tried
devenv "full-path-to-sln-file.sln"
but it didn't work.
How do I do this?
You can just use explorer.exe !!
In your command line type in:
explorer mysolution.sln
Use the full path to the devenv.exe executable and the full path to the sln solution file, both wrapped in quotes and with a space in between. If your solution file is in a network path, make sure that it does not require authentication before accessing the destination folder.
C:\Users\YourWindowsUser>"D:\Visual Studio\Common7\IDE\devenv.exe" "\\networkDirectory\profiles\Desktop\VisualStudioSolutions\Project999.sln"
I wrote an open source application called OpenVSSolution to do exactly this.
Essentially:
Put this helper exe somewhere in your PATH
The exe scans the current directory for a .sln file
The exe opens devenv passing in the sln file
The explanation is on here:
https://davemateer.com/coding/2018/11/14/Open-visual-studio-from-command-line.html
I find this incredibly useful and is how I open all solutions.
You can open a solution file with Visual Studio directly if you are within a folder that contains the solution file, looking for it recursively (Powershell 7.0) by doing the following:
Open powershell, type
echo $profile
Open up the location of your profile, save the below into it:
function vs
{
Get-ChildItem *.sln -Recurse | Invoke-Item
}
Then just type vs into a folder and it will go through the sub directories looking for the solution file and open one if found
Try WhatsNew. From the readme:
Why fish around for Visual Studio solution files using Windows Explorer when you can find and launch them from your terminal window with just three little letters? Run sln (an alias for Open-Solution) to recursively search your current working directory for a solution file and launch it, if one is found.
for macOs I just use:
open projectname.csproj
it will open it in visual studio 2019 for me

How can I use the cl command on PowerShell?

I am unable to use the cl command in PowerShell.
I tried to add the following command to my PowerShell profile to exec vcbuildtools.bat, but PowerShell does not recognize cl command on PowerShell?
&"C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"
OS: Windows 10
Just to be clear I'm addressing the asker's issue that cl is not in the PATH even after running this in PowerShell
&"C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"
I think this boils down to the issue that batch file can't export variables to PowerShell (also related: this question), as you've found out with vcbuildtools.bat. I think it's because PowerShell invokes a cmd.exe subshell to execute the batch file which changes the environment in the subshell but the changes don't propagate to the parent shell i.e. PowerShell.
Solution 1
One way is to use the fact that subshell inherits the environment from the parent shell. So if you run this in PowerShell, the environment set by the batch file is preserved
cmd.exe /k "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" `& powershell
Take note of `&. The character has to be escaped because it has a special meaning in PowerShell.
Solution 2
The Pscx module has an Import-VisualStudioVars function which imports environment variables for Visual Studio. An example usage is
Import-VisualStudioVars 2015 amd64
if you're using VS/BuildTools 2015 and compiling 64-bit programs. You can use Pop-EnvironmentBlock to revert the changes. See man Import-VisualStudioVars -full for more information.
Alternatively, Pscx also has an Invoke-BatchFile function that retains environment changes by a batch file. An example usage
Invoke-BatchFile "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"
See man Invoke-Batchfile -full for more information.
Notes
To download the up-to-date version of Pscx from PowerShell gallery, you will need PowerShellGet which is shipped with PowerShell 5 and is available as a downloadable installer for PowerShell 3 and 4.
For those with PowerShell 1 and 2, older versions of Pscx is available on Codeplex.
You can use the following function to invoke a cmd.exe shell script (batch file) and persist its environment variables:
function Invoke-CmdScript {
param(
[String] $scriptName
)
$cmdLine = """$scriptName"" $args & set"
& $env:SystemRoot\system32\cmd.exe /c $cmdLine |
Select-String '^([^=]*)=(.*)$' | ForEach-Object {
$varName = $_.Matches[0].Groups[1].Value
$varValue = $_.Matches[0].Groups[2].Value
Set-Item Env:$varName $varValue
}
}
Add this function to your PowerShell profile, and run the batch file using the function:
Invoke-CmdScript "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\vsvars32.bat"
Fortunately, VS 2019 Community now has a Developer PowerShell for VS 2019 command.
The actual command, if you want to see the properties for the shortcut, is rather verbose.
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 14bbfab9}"
Anyway, I am using this and it adds the right cl.exe to my path, but there is an odd message after running it:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\ostream(750): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
.\hey.cpp(4): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::operator <<<std::char_traits<char>>(std::basic_ostream<char,std::char_traits<char>> &,const char *)' being compiled
Another option from PowerShell gallery:
posh-vs
Makes Visual Studio command line tools available in PowerShell. Supports Visual Studio 2017 and 2015.
I also encountered the same problem, type cmd.exe and you'll change control to command line.
PowerShell example
If you want to go back to PowerShell, no problem. Just write exit. As simple as it sounds

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

Is there a command line option like devenv.exe /Edit for Sql Server Management Studio

I'm using visual studio's external tools to open .sql scripts in Sql Server Management Studio (SSMS). The problem is, each time I use the external tools command to open a .sql file from visual studio, it opens a new instance of SSMS.
Visual Studio has a switch /Edit that will do this, is there one for SQL Server Management Studio?
Choose to open the file with Explorer[1] instead of SSMS. That way the system will search for any existing instances of SSMS first.
[1] %windir%\explorer.exe
I don't think the problem is with Visual Studio external tool command. Look at SSMS command line options - maybe there is a way to force reusing existing SSMS instance.
The following works for me ( I am SQL Server 2008 though) :
So the real answer I quess is to use cmdow
Edit: After more testing I realized the following :
First open the files with connenction with: ( remove any enters while copying, this is one liner )
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\
Common7\IDE\Ssms.exe" "C:\Users\yordgeor\Desktop\Trigger.sql"
"C:\Users\yordgeor\Desktop\Trigger1.sql" -S ysg -d poc_dev -E -nosplash
In this example it opens two files ( but you could open less of course ; )
after which , No matter how many times I run
cmdow /Run "C:\Users\yordgeor\Desktop\Trigger1.sql"
cmdow /Run "C:\Users\yordgeor\Desktop\Trigger2.sql"
cmdow /Run "C:\Users\yordgeor\Desktop\Trigger3.sql"
it opens the same instance of the Microsoft Sql Server Management Studio with the same connection
you could open at once at the command line many files , but I quess you asked for the upper answer.
So you could find the path to the Ssms.exe by:
cd %ProgramFiles%
dir *ssms.exe /s /b
so the syntax of the command is:
pathToTheExe pathToFile1 pathToFile2 -S serverName -d DatabaseToConnectTo -E (toUseWindowsAuthentication) -nosplash
After 20 seconds of googling I cheated from here:
The correct answer is no. The SSMS has a limited set of options, and although it uses the VS framework, does not support the edit command. This could be an interesting feature to add.
I am assuming you need to open these scripts in SSMS to be able to run them as well, otherwise I would suggest pointing them to Visual Studio directly since it does support syntax highlighting. However it will not solve your problem.
If you associate that file extension with Visual Studio, then VS should open it.
If you're finding that VS starts a new instance every time, then you need to specify the /edit command line option.
Take a look at http://stevedunns.blogspot.com/2009/03/programs-that-launch-or-should-launch.html for more information.

Resources