Use PowerShell for Visual Studio Command Prompt
Asked Answered
M

7

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?

Minatory answered 9/12, 2010 at 12:22 Comment(1)
possible duplicate of How I can use PowerShell with the Visual Studio Command Prompt?Mohair
D
14

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.

Disfeature answered 9/12, 2010 at 12:44 Comment(0)
M
3

have a look at PowerConsole

Morula answered 9/12, 2010 at 12:25 Comment(1)
Ouch, @Yoni, I even have PowerConsole on my laptop, but have never used it since installation. I suppose now is a good time.Minatory
I
3

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

Idalia answered 9/12, 2010 at 13:43 Comment(0)
P
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
Propagable answered 11/12, 2010 at 14:18 Comment(0)
E
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.

Eirena answered 9/12, 2010 at 19:14 Comment(0)
B
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

Buschi answered 13/5, 2021 at 9:44 Comment(1)
what is 1652063a?Klopstock
A
0
function msvc_env {
    param (
        [string]$arch = "amd64",
        [string]$vcvars_ver = "14.3"
    )

    $vcvarsall = "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat"
    cmd /K "`"$vcvarsall`" $arch -vcvars_ver=$vcvars_ver && pwsh.exe"
}
Anderlecht answered 22/7 at 18:55 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Barcellona

© 2022 - 2024 — McMap. All rights reserved.