Skip to content

Instantly share code, notes, and snippets.

@ArthurHNL
Created September 23, 2019 07:37
Show Gist options
  • Save ArthurHNL/b29f008cbc7a159a6ae82a7152b1cb2a to your computer and use it in GitHub Desktop.
Save ArthurHNL/b29f008cbc7a159a6ae82a7152b1cb2a to your computer and use it in GitHub Desktop.
Enter VSDevShell from a stand-alone powershell script
$vsPath = &(Join-Path ${env:ProgramFiles(x86)} "\Microsoft Visual Studio\Installer\vswhere.exe") -property installationpath
Import-Module (Join-Path $vsPath "Common7\Tools\vsdevshell\Microsoft.VisualStudio.DevShell.dll")
Enter-VsDevShell -VsInstallPath $vsPath -SkipAutomaticLocation
@sajisV5Framework
Copy link

Does not work on Visual Studio 2017 Professional.

@ArthurHNL
Copy link
Author

I made it for 2019.

@sajisV5Framework
Copy link

Any hint on how to make a generic script that works with any version of visual studio ?

@Antebios
Copy link

On line #2 remove "vsdevshell" and then it works for me in Visual Studio 2019.

Import-Module (Join-Path $vsPath "Common7\Tools\Microsoft.VisualStudio.DevShell.dll")

@howlettt
Copy link

howlettt commented Dec 1, 2020

A slightly more future proof line #2:
Import-Module (Get-ChildItem $vsPath -Recurse -File -Filter Microsoft.VisualStudio.DevShell.dll).FullName

@AlQuemiste
Copy link

A full-blown script, pwsh-dev-x64.ps1:

# usage:
# $ pwsh-dev-x64.ps1 [MSVC|Clang]

param(
    [String] $Compiler = "MSVC"
)

if ($Compiler -ne "MSVC" -and $Compiler -ne "Clang") {
    Write-Error "Unknown compiler '$Compiler'; must be MSVC or Clang"
    Exit -1
}

Write-Host "Setting up environment variables..."

# Visual Studio path <https://github.com/microsoft/vswhere/wiki/Find-VC>
$vsPath = &"${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationpath

Write-Host "Microsoft Visual Studio path = '$vsPath'"

# Use module `Microsoft.VisualStudio.DevShell.dll`
Import-Module (Get-ChildItem $vsPath -Recurse -File -Filter Microsoft.VisualStudio.DevShell.dll).FullName

Enter-VsDevShell -VsInstallPath $vsPath -SkipAutomaticLocation -DevCmdArguments '-arch=x64'

# NOTE: `-DevCmdArguments` are arguments to `vsdevcmd.bat`

# Select compiler
if ($Compiler -eq "MSVC") {
    $_Compiler = "MSVC"
    Set-Item -Path "env:CC" -Value "cl.exe"
    Set-Item -Path "env:CXX" -Value "cl.exe"
}
elseif ($Compiler -eq "Clang") {
    $_Compiler = "Clang"
    Set-Item -Path "env:CC" -Value "clang-cl.exe"
    Set-Item -Path "env:CXX" -Value "clang-cl.exe"
}

Write-Host "Selecting $_Compiler as C/C++ compiler."

@zaufi
Copy link

zaufi commented Jun 11, 2022

Doesn't work for VS Build Tools (i.e. when no IDE is installed).

@richyk1
Copy link

richyk1 commented May 10, 2024

@AlQuemiste Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment