Skip to content

Instantly share code, notes, and snippets.

@hoyhoy
Last active December 3, 2025 23:11
Show Gist options
  • Select an option

  • Save hoyhoy/7e63c5b50732bda4dd9419bf709fdada to your computer and use it in GitHub Desktop.

Select an option

Save hoyhoy/7e63c5b50732bda4dd9419bf709fdada to your computer and use it in GitHub Desktop.
PS1 Script to Generate the Conan settings.yml from a Developer Powershell for VS 2022 or VS 2026
# Get the value of the VSCMD_VER environment variable
$vsCmdVersion = $env:VSCMD_VER
# Check if VSCMD_VER is set
if (-not $vsCmdVersion) {
Write-Host "Error: VSCMD_VER environment variable is not set or is empty." -ForegroundColor Red
exit 1
}
# Extract the major and minor version (e.g., "17.13" from "17.13.2")
$msvcVersion = ($vsCmdVersion -split '\.')[0] -join '.'
# Extract the major version (e.g., "17" from "17.13.2")
$msvcMajorVersion = ($vsCmdVersion -split '\.')[0] -join '.'
# Define the mapping of MSVC versions to their corresponding version and update numbers
$msvcVersionMap = @{
"17.14" = @(194, 4) # compiler.version = 194, compiler.update = 4
"17.15" = @(194, 5) # compiler.version = 194, compiler.update = 5
"17.16" = @(194, 5) # compiler.version = 194, compiler.update = 6
"18.0" = @(195, 0) # compiler.version = 195, compiler.update = 0
"18.1" = @(195, 1) # compiler.version = 195, compiler.update = 1
"18.2" = @(195, 2) # compiler.version = 195, compiler.update = 2
"18.3" = @(195, 3) # compiler.version = 195, compiler.update = 3
"18.4" = @(195, 4) # compiler.version = 195, compiler.update = 4
"18.5" = @(195, 5) # compiler.version = 195, compiler.update = 5
"18.6" = @(195, 6) # compiler.version = 195, compiler.update = 6
"18.7" = @(195, 7) # compiler.version = 195, compiler.update = 7
"18.8" = @(195, 8) # compiler.version = 195, compiler.update = 8
"18.9" = @(195, 9) # compiler.version = 195, compiler.update = 9
"18.10" = @(195, 10) # compiler.version = 195, compiler.update = 10
}
$msvcVersionToolsetMap = @{
194 = "v143" # there is no v144
195 = "v145"
}
if ($msvcMajorVersion -eq "18") {
write-host "`nVisual Studio 2026 Command Prompt variables set." -ForegroundColor Yellow
} else {
write-host "`nVisual Studio 2022 Command Prompt variables set." -ForegroundColor Yellow
}
# Debugging: Output the extracted version for verification
# Write-Host "Extracted MSVC Version: $msvcVersion" -ForegroundColor Cyan
# Get the corresponding version and update numbers from the map
if ($msvcVersionMap.ContainsKey($msvcVersion)) {
$compilerVersion = $msvcVersionMap[$msvcVersion][0] # First element of the array
$compilerUpdate = $msvcVersionMap[$msvcVersion][1] # Second element of the array
# Output the results
$versionParts = $env:VCToolsVersion.Split('.')
$compilerRuntime = "dynamic"
Write-Host "[settings]" -ForegroundColor Magenta
Write-Host "os=windows" -ForegroundColor DarkGray
Write-Host "arch=x86_64" -ForegroundColor DarkGray
Write-Host "compiler=msvc" -ForegroundColor DarkGray
Write-Host "compiler.cppstd=20" -ForegroundColor DarkGray
Write-Host "compiler.version=$compilerVersion" -ForegroundColor DarkGray
Write-Host "compiler.update=$compilerUpdate" -ForegroundColor DarkGray
Write-Host "compiler.toolset=$compilerToolset" -ForegroundColor DarkGray
Write-Host "compiler.runtime=$compilerRuntime" -ForegroundColor DarkGray
# Get the Windows SDK version from the environment variable
$winSdkVersion = $env:WindowsSDKVersion
# Clean the Windows SDK version (remove trailing backslashes and non-numeric/non-period characters)
$cleanedSdkVersion = $winSdkVersion -replace '[^0-9.]', ''
# Additional outputs
$vsVersion = ($msvcVersion -split '\.')[0] # Extract "17" from "17.13"
Write-Host "[conf]" -ForegroundColor Blue
Write-Host "tools.microsoft:winsdk_version=$cleanedSdkVersion" -ForegroundColor Blue
Write-Host "tools.microsoft.msbuild:vs_version=$vsVersion" -ForegroundColor Blue
Write-Host "tools.microsoft:msvc_update=$compilerUpdate" -ForegroundColor Blue
} else {
Write-Host "Error: VSCMD_VER '$vsCmdVersion' (extracted as '$msvcVersion') not found in the mapping." -ForegroundColor Red
Write-Host "Available versions in the mapping: $($msvcVersionMap.Keys -join ', ')" -ForegroundColor Yellow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment