Skip to content

Instantly share code, notes, and snippets.

@sandyarmstrong
Last active August 10, 2021 21:21
Show Gist options
  • Save sandyarmstrong/8b48d7dd0cb01c63cabc6b7db8d8ac10 to your computer and use it in GitHub Desktop.
Save sandyarmstrong/8b48d7dd0cb01c63cabc6b7db8d8ac10 to your computer and use it in GitHub Desktop.
Setting up a modern PowerShell+VS experience on Windows

Install the new Windows Terminal.

Install latest stable PowerShell

Get it from https://github.com/PowerShell/PowerShell/releases/latest . Visual Studio still uses the old PowerShell by default. What you want is PowerShell 7 or greater. This used to be known as PowerShell Core. It's cross-platform, open source, and just better.

Load VS utilities in PowerShell

In order to locate your VS install, grab the VSSetup PowerShell module with Install-Module VSSetup -Scope CurrentUser. Next, from a PowerShell terminal, open $PROFILE in your favorite editor to start improving it.

You can then use the Enter-Vs function in my sample $PROFILE to put yourself in a VS dev environment. This takes a couple of seconds, so I only run it when I need it. But if you prefer to always have VS command line tools ready, just add a call to Enter-Vs at the end of your $PROFILE.

Ergonomics

If you're used to unix shells, you may also consider installing PSReadLine and adding the following to your profile:

Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadlineKeyHandler -Key Tab -Function Complete

You can install PSReadLine like so:

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

Next Steps

Make stuff prettier if you like following instructions from https://www.hanselman.com/blog/HowToMakeAPrettyPromptInWindowsTerminalWithPowerlineNerdFontsCascadiaCodeWSLAndOhmyposh.aspx

I've attached my PowerShell profile and Windows Terminal settings files for reference.

Import-Module posh-git
Import-Module oh-my-posh
Set-PoshPrompt -Theme Paradox
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadlineKeyHandler -Key Tab -Function Complete
function Enter-Vs
(
[switch]$Prerelease
)
{
Import-Module VSSetup
$vsInstall = Get-VSSetupInstance -All -Prerelease:$Prerelease | Select-VSSetupInstance -Require 'Microsoft.Build' -Latest
if (-not $vsInstall) {
Write-Error "No matching VS environment found; check installer"
return
}
# # Or, if you don't want to install VSSetup:
# $vsInstalls = . "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -format json | ConvertFrom-Json
# $vsInstall = $vsInstalls[0]
Import-Module (Join-Path $vsInstall.installationPath "Common7" "Tools" "Microsoft.VisualStudio.DevShell.dll")
Enter-VsDevShell $vsInstall.instanceId
}
// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"profiles": {
"defaults":
{
"fontFace": "Cascadia Code PL",
"fontSize": 10
},
"list": [
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "PowerShell Core",
"source": "Windows.Terminal.PowershellCore",
"fontFace": "Cascadia Code PL"
},
{
// Make changes here to the powershell.exe profile
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
},
{
// Make changes here to the cmd.exe profile
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "cmd",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
"hidden": false,
"name": "Ubuntu-20.04",
"source": "Windows.Terminal.Wsl"
}
]
},
// Add custom color schemes to this array
"schemes": [],
// Add any keybinding overrides to this array.
// To unbind a default keybinding, set the command to "unbound"
"keybindings": [
{
"command": {"action": "splitPane", "split": "auto", "splitMode": "duplicate"},
"keys": "alt+shift+d"
},
{
"command": "closePane",
"keys": "ctrl+w"
},
{
"command": "find",
"keys": "ctrl+shift+f"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment