Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell Profile
# .NET build
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
# Set this to the value returned by 'vswhere -property instanceId'
$vsInstanceId = "02218237"
# If PowerShell module exists, prefer that (could be in either of these places apparently)
If (Test-Path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\Microsoft.VisualStudio.DevShell.dll") {
Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell -InstanceId $vsInstanceId -ErrorAction SilentlyContinue
} elseif (Test-Path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\vsdevshell\Microsoft.VisualStudio.DevShell.dll") {
Import-Module "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\vsdevshell\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell -InstanceId $vsInstanceId -ErrorAction SilentlyContinue
} else {
$places = @{
"$($env:VS140COMNTOOLS)VSVars32.bat" = "2015"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\Tools\VsDevCmd.bat" = "2017"
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\Tools\VsDevCmd.bat" = "2019"
}
$places.GetEnumerator() | Sort-Object -Descending -Property Value | Where-Object { Test-Path $_.Key } | Select-Object -First 1 | ForEach-Object {
cmd /c "`"$($_.Key)`" & set" |
ForEach-Object {
if ($_ -match "=") {
$v = $_.split("=")
set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"
}
}
Write-Host "`nVisual Studio $($_.Value) Command Prompt variables set." -ForegroundColor Yellow
$prefix = ""
if ($isAdmin) {
$prefix = "Administrator: "
}
[System.Console]::Title = "$($prefix)Visual Studio $($_.Value) Windows Powershell"
}
}
# Remember directory
Register-EngineEvent PowerShell.Exiting -Action {
Write-Warning "Saving current location"
[System.Environment]::SetEnvironmentVariable("LocationMemory", (Get-Location).Path, [System.EnvironmentVariableTarget]::User)
} | Out-Null
$lastPath = [System.Environment]::GetEnvironmentVariable("LocationMemory",[System.EnvironmentVariableTarget]::User)
if (($lastPath -ne $null) -and (Test-Path $lastPath)) {
Set-Location $lastPath
}
# PoshGit
if (Get-Module -ListAvailable -name posh-git) {
Import-Module posh-git
# Need this so posh-git tab expansion still works (even though we're not using the status)
$env:POSH_GIT_ENABLED = $true
}
# Posh Docker
if (Get-Module -ListAvailable -name posh-docker) {
Import-Module posh-docker
}
# Posh NPM
if (Get-Module -ListAvailable -name posh-npm) {
Import-Module posh-npm
}
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
if (Get-Module -ListAvailable -name oh-my-posh) {
Import-Module oh-my-posh
$configFile = [environment]::GetFolderPath("MyDocuments") + "\ohmyposh.json"
if (Test-path $configFile) {
set-poshprompt $configFile
}
}
if (Get-Module -ListAvailable -name Terminal-Icons) {
Import-Module -Name Terminal-Icons
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment