Writing Scripts in VS Code

VS Code with Microsoft's free PowerShell extension colors a script, suggests commands as you type, underlines problems such as a missing quote or an alias before you run anything, and runs the whole script with F5 in a terminal inside the editor.

Apprentice · PowerShell · Updated

After this lesson you can

  • Open a folder of scripts in VS Code with code ., and run a script with F5 in the PowerShell extension's terminal
  • Read a problem VS Code underlines in a script, such as a missing quote or an alias, and fix the line it marks

Key takeaways

  • VS Code is a free code editor for Windows, Macs and Linux, and Microsoft's PowerShell extension teaches it PowerShell. Both are optional: edit still works.
  • code . opens the folder you're in, with its files listed on the left. The extension colors each part of a script and suggests commands as you type.
  • A red wavy line is a mistake PowerShell can't read, such as a missing quote. A script with one doesn't run at all, not even its first lines.
  • A yellow wavy line is advice from PSScriptAnalyzer, such as using a full command name instead of an alias. Point at the line to read the message.
  • F5 saves the script and runs it in the terminal at the bottom of the window. F8 runs only the lines you've selected.

edit is fine for a three-line script. But the scripts in this module grow: a backup that checks before it copies, a loop over thirty files, a menu. At that size you want what a word processor gives you for an essay: colors that set the parts apart, line numbers, and a spell checker that underlines a mistake before you hand it in. For PowerShell scripts, that's VS Code with the PowerShell extension. Both are free and both are optional: if edit or Notepad suits you, keep using it. Every lesson shows a script's contents, not the editor it was typed in. VS Code is also what many developers and system administrators use every day, so it's worth knowing your way around.

VS Code and the PowerShell extension

VS Code (Visual Studio Code) is a free code editor from Microsoft for Windows, Macs and Linux: a text editor made for code. Download it from code.visualstudio.com, or install it with winget (Installing Apps and Modules: winget and the PowerShell Gallery):

winget install Microsoft.VisualStudioCode

On its own, VS Code knows a little about PowerShell. The PowerShell extension teaches it the rest. In VS Code, click the Extensions icon on the left (or press Ctrl+Shift+X), search for PowerShell, and check the publisher before you click Install: it should say Microsoft, the same habit as checking who published a module.

Windows also comes with an older editor, the Windows PowerShell ISE. It works only with Windows PowerShell 5.1 and gets no new features; Microsoft recommends VS Code instead.

Open your scripts folder: code .

In PowerShell, go to the scripts folder from A Script Is an Application and open it, . being "this folder":

Set-Location "$HOME\terminal-practice\scripts"
code .

VS Code opens with the folder's files listed on the left. The first time, it may ask whether you trust the authors of the files in this folder: they're yours, so choose Yes. Click the New File icon beside the folder's name, call the file greeting.ps1, and type these lines:

# greeting.ps1: says hello, then lists the scripts in my practice folder.
Write-Output "Hello, $env:USERNAME!"
gci "$HOME\terminal-practice\scripts" -Name

As you type, the extension colors each part: comments in one color, commands in another, text in quotes in a third. Every line has a number, so an error about line 3 takes a second to find. Type Get-Ch and a list of commands appears under the cursor, the same list Ctrl+Space shows at the prompt: press Tab or Enter to take one.

Problems underlined as you type

A wavy line under part of your script means VS Code has spotted something. Point at it with the mouse, and a box shows the message. Ctrl+Shift+M opens the Problems panel, which lists every one with its line number.

Look at gci on line 3: it has a yellow wavy line. That's advice from PSScriptAnalyzer, a linter: a program that reads your code without running it and points out lines that are likely to cause trouble. The extension brings it with it. Its message is:

'gci' is an alias of 'Get-ChildItem'. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content.

It's the rule from Short Names: Aliases: short names at the prompt, full names in scripts, so other people (and you in six months) can read them. Change gci to Get-ChildItem, and the line disappears.

VS Code with greeting.ps1 open: gci on line 3 has a yellow wavy underline, and a box above it shows PSScriptAnalyzer's message that 'gci' is an alias of 'Get-ChildItem'

A red wavy line is more serious: something PowerShell can't read at all. Delete the closing " after scripts on line 3, and "$HOME\terminal-practice\scripts -Name gets one. Here's what running that script would give you:

ParserError: C:\Users\Sam\terminal-practice\scripts\greeting.ps1:3
Line |
   3 |  Get-ChildItem "$HOME\terminal-practice\scripts -Name
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | The string is missing the terminator: ".

The terminator is the closing quote. Notice that nothing ran, not even the greeting on line 2: PowerShell reads the whole script before it starts, and a script it can't read doesn't run at all. VS Code shows you the same message the moment you type, before you run anything. Put the " back.

F5: run it inside the editor

Press F5. VS Code saves the script and runs it in a terminal that opens at the bottom of the window, running PowerShell 7, and the output appears under your code:

Hello, Sam!
greeting.ps1
hello.ps1

Yours shows your own name, and any other scripts you made in earlier lessons. To run only part of a script, select the lines and press F8. You can type commands into that terminal too, the same as any PowerShell window; Ctrl+` (the backquote key, above Tab) shows or hides it.

On a Mac, the keys use Cmd where Windows uses Ctrl. To get code at the Mac's prompt, open VS Code, press Cmd+Shift+P, type shell command, and choose Install 'code' command in PATH.

Check yourself

2 questions to try on the lesson page.

So what

VS Code shows your script in color, underlines a mistake before it bites, and runs the script without leaving the editor. It's a more comfortable desk, not a different job: everything you write runs the same from any PowerShell window.

Key terms

Cheat sheets

Related