21

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?

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • 1
    possible duplicate of [How I can use PowerShell with the Visual Studio Command Prompt?](http://stackoverflow.com/questions/2124753/how-i-can-use-powershell-with-the-visual-studio-command-prompt) – Mark Meuer Jun 25 '15 at 16:24

6 Answers6

13

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.

Community
  • 1
  • 1
Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
3

have a look at PowerConsole

Yoni H
  • 453
  • 2
  • 7
  • Ouch, @Yoni, I even have PowerConsole on my laptop, but have never used it since installation. I suppose now is a good time. – ProfK Dec 09 '10 at 13:29
  • 1
    PowerConsole has been incorporated into NuGet http://nuget.codeplex.com/. You get PowerShell inside Visual Studio and a package management system. – Doug Finke Dec 09 '10 at 13:41
2

PowerConsole has been incorporated into NuGet http://nuget.codeplex.com/. You get PowerShell inside Visual Studio and a package management system.

Doug Finke
  • 6,675
  • 1
  • 31
  • 47
2

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
mjsr
  • 7,410
  • 18
  • 57
  • 83
1

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.

zdan
  • 28,667
  • 7
  • 60
  • 71
0

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

sunny moon
  • 1,313
  • 3
  • 16
  • 30
  • what is `1652063a`? – JustWe May 18 '21 at 03:09
  • 1
    There's a folder named `1652063a` under `C:/ProgramData/Microsoft/VisualStudio/Packages/_Instances/`, go figure what it means. An instance of Microsoftish way of naming things, I guess. – sunny moon May 19 '21 at 21:54