Getting started with PowerShell scripting requires the right tools, and Visual Studio Code (VS Code) is one of the best editors for PowerShell development.
This tutorial will walk you through downloading and setting up VS Code specifically for PowerShell, configuring the terminal, installing essential extensions, and exploring some of VS Code’s powerful features.
Follow along, and by the end, you’ll have a fully configured environment ready for scripting with PowerShell.
Downloading and Installing VS Code
Let’s get serious and do some real work! But first, we’ll download the VS Code editor.
To download and install VS Code:
1. Look up VS Code in your favorite search engine and click the Download link in the upper-right corner.
2. Pick your operating system and architecture (x64 or Arm64), and wait for the download to complete.
The choice here is the Windows x64 System Installer. With this option, you don’t have to worry about profiles if you use another user account at some time.
3. Once downloaded, start the installation and launch VS Code.
We now have VS Code and are ready to configure it for PowerShell development.
Configuring VS Code for PowerShell
The default screen size of VS Code works for most folks, but you can make VS Code’s view a bit bigger as you like.
Before you get lost in navigating VS Code, you can familiarize yourself with its user interface first.
1. Press Ctrl+= and Ctrl+- to zoom in and out if you need to make the entire window bigger or smaller.
That’s better!
2. Now, hit Ctrl+N to create a new tab, and you’ll see VS Code wants to know what language we’re working with.
VS Code can typically autodetect this, but since there’s no code in the editor window, it doesn’t know.
3. Type some PowerShell code in there.
Get-Service -Name 'wuauserv'
Notice that nothing has changed. Why? Because VS Code treats this code as plain text and doesn’t understand PowerShell code yet. Let’s change that.
4. Install an extension by clicking the Extension icon on the Activity Bar (far left-hand) and searching for PowerShell.
You’ll see that VS Code has hundreds of extensions for almost anything.
Click Install, wait a few seconds, and the extension should be installed.
Exploring VS Code Features
With the extension installed, you can explore VS Code features for PowerShell development. While there are many other options to cover, remember this is a PowerShell guide.
1. Click back to the previous tab and save the code with a PS1 extension, the file extension for PowerShell scripts.
After doing so, notice two things: the text colors changed (known as syntax highlighting), and a PowerShell terminal opened below.
- Syntax highlighting helps you read the code better, especially when writing large scripts.
- The PowerShell console below, the terminal window, is a built-in shell integrated into VS Code, so you don’t have to leave the editor.
2. Paste some simple code to the terminal to highlight some features.
$users = @{ 'abertram' = 'Adam Bertram' 'bjoel' = 'Billy Joel' 'bbrown' = 'Bobby Brown' } echo "adbertram's full name is $($users['abertram'])"
Notice the squiggly lines under echo. VS Code helps you write better code.
In this instance, VS Code highlights an alias of `echo` and suggests you change it to the actual command name for clarity.
3. Now, hover over the alias, and you’ll see that it tells you to use `Write-Output` instead.
Click on Quick Fix right there to quickly fix it.
4. Something else is code collapsing. If you hover over any collapsible code, VS Code will show a down arrow.
You can click this arrow to collapse and expand code, hiding code you’re not working on.
5. If you wish to play around with the settings:
– Navigate to the File menu → Preferences → Settings.
– From the Settings tab, expand Extensions and choose PowerShell.
- Navigate to the File menu → Preferences → Settings.
- From the Settings tab, expand Extensions and choose PowerShell.
You’ll see many things you can tweak and change.
Configuring the Integrated Terminal
One last thing you’ll need to know: the terminal. The integrated terminal below the editor is a shell-like cmd.exe, PowerShell, Bash, etc., directly integrated into VS Code. Since it’s part of VS Code, you don’t have to go out of the editor to test code, offering many advantages.
VS Code uses Windows PowerShell as the default terminal when writing PowerShell scripts. Since we’re using PowerShell Core, we’ll need to configure VS Code to use PowerShell Core instead of Windows PowerShell.
To configure VS Code:
1. Hit Ctrl+Shift+P, type settings, then find and click Preferences: Open User Settings (JSON).
You’ll see this JSON file opens on the editor. VS Code uses JSON in the background, making it a quick way to make changes.
2. In this case, we must create a new integrated profile with two entries and set it to PowerShell 7.
- Create the integrated profile, pointing it to the PowerShell 7 executable.
- Set the default profile as the one just created.
"terminal.integrated.profiles.windows": { "PowerShell Core": { "path": ["C:\Program Files\PowerShell\7\pwsh.exe"], "icon": "terminal-powershell" } }, "terminal.integrated.defaultProfile.windows": "PowerShell Core"
3. Save the settings.json file and reopen VS Code.
4. Now, click into the terminal at the bottom and type `$PSVersionTable` to check the PowerShell version VS Code uses.
You’ll see that the PSVersion shows as 7.4.3, and the PSEdition is Core, precisely as it should be.
Running PowerShell Scripts on the Terminal
The terminal can do many things, but executing the code in the window is the most convenient. For example, let’s say you want to run a script to test it out.
Once you’ve adjusted and saved the script, hit F5. It will run and show output directly in the terminal below.
But perhaps you only want to run a part of it. To do so, you can highlight a piece of the code and hit F8, which only runs that part of the script.
This ability to execute only a portion of the script is useful when troubleshooting larger scripts.
Conclusion
You’ve now set up VS Code for PowerShell development and explored its essential features. With the PowerShell extension, integrated terminal, and useful editing tools, you can start scripting and troubleshooting directly in VS Code.
As you become comfortable with the interface, explore additional settings and extensions to customize your development environment further. VS Code is a powerful editor and can do so much, but for a beginner, you have to crawl before you can walk!