Skip to content

Instantly share code, notes, and snippets.

@flcdrg
Last active February 25, 2024 07:29
Show Gist options
  • Save flcdrg/030c4151a019dcf5447c3c3b45764c15 to your computer and use it in GitHub Desktop.
Save flcdrg/030c4151a019dcf5447c3c3b45764c15 to your computer and use it in GitHub Desktop.
PowerShell Profile
# No profile for VS Code
if (Test-Path env:TERM_PROGRAM) {
return
}
# .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"
}
$configFile = [environment]::GetFolderPath("MyDocuments") + "\ohmyposh.json"
oh-my-posh init pwsh --config $configFile | Invoke-Expression
if (Get-Module -ListAvailable -name Terminal-Icons) {
Import-Module -Name Terminal-Icons
}
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment