💾 Archived View for pwshnotes.flounder.online › gemlog › 2021-08-19-installing-vs-code.gmi captured on 2021-12-03 at 14:04:38. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
PowerShell is an interpreted scripting language. It can be typewritten and executed directly in a shell. Or it can be typed into a plain text file and executed later. This file is called a script. PowerShell scripts use the file extension "PS1". Any text editor, such as Notepad, can be used to write PowerShell code.
A programming language might have a dedicated editor called an Integrated Development Environment or IDE. This software provides enhancements related to editing code.
Common IDE features are:
The version of PowerShell which ships with Windows comes bundled with its own IDE called Windows PowerShell Integrated Scripting Environment (ISE). However, ISE is deprecated. That means ISE is not receiving new features or fixes even though it is still available.
The successor to ISE is Visual Studio Code (VS Code) plus the PowerShell extension. This combination provides all the IDE features mentioned to PowerShell authors. And these two projects are where Microsoft is focusing its attention and providing updates.
Today, I will install VS Code and the PowerShell extension. Then we will create, edit, and run a simple PowerShell script within VS Code.
Since I previously installed PowerShell 7 on my computer, the PowerShell extension in VS Code will load PowerShell 7 by default. I covered installing PowerShell in a previous gemlog. See link below.
It is possible to search for and install extensions without leaving VS Code. And it is possible to visit the Visual Studio Marketplace in a Web browser and to install an extension from the store. Today, I will demonstrate the former and use the Extensions view within VS Code to install my extension.
VS Code has many options and functions. And there is only one, unifying interface for these which is VS Code's command-line interface: known as the Command Palette. After activating the palette, a small command line and search results will be overlaid on your workspace. While VS Code has menus, keyboard shortcuts, and side panels; everything appears in the Command Palette. When I need access to a setting or IDE function, I will refer to the Command Palette. It is a good idea to get accustomed to the Command Palette in VS Code.
Fortunately, the Command Palette will show suggestions based on partial names. It also displays recently used commands. So you can repeat commands by simply selecting an entry with your arrow keys and pressing Enter. One tip is that commands require a leading '>' which is provided by default. Backspacing over that will show you a list of open tabs, recent files, or let you open a file by typing its path. If you want to search the list of commands again, you must re-add the greater-than sign to the start.
The default keyboard shortcuts for the Command Palette are Ctrl+Shift+P and F1. The Command Palette can also be reached from the menu via View > Command Palette. You might find it useful to set a more convenient keyboard shortcut for the palette. I will cover changing a keyboard shortcut later.
VS Code is now installed.
VS Code must be running before we can install an extension.
Now VS Code is running.
We will use the Command Palette to access the Extensions view.
The PowerShell extension is now installed.
One option you have is to activate the classic PowerShell ISE color theme included with the PowerShell extension. Activating a color theme makes no functional changes. But for those who remember ISE or who prefer a light color theme then this might be a useful option.
If you still have the PowerShell marketplace tab open after installing the extension, you can simply click the 'Set Color Theme' button shown near the top of the page. It is also possible to use the Command Palette to activate the theme which I cover next.
Let us go through the process of creating a new PowerShell script file, typing some commands, and running the script. This will give us the basic skills to create scripts for ourselves in the future.
Because VS Code is used with many programming languages, it needs some way to recognize what language we're using before it can load the relevant features. There are two ways to set the language mode. If we create an untitled file, VS Code embeds a hint in the file asking us to select a language mode. Alternatively, VS Code will automatically set the language based on the file extension. I will demonstrate creating a file with the PowerShell file extension.
We need VS Code to be running.
Now VS Code is running.
$hello = "hello world" Out-Host -InputObject $hello
Notice the syntax highlighting applied to the code. The variable $hello appears in light red. The string "hello world" appears in dark red. The cmdlet Out-Host appears in blue. And the parameter -InputObject appears in black. Syntax highlighting is one of the features which depends on setting the correct language mode.
Let's save our file.
Let's run our file.
Assuming there are no typos, the script will run and the output, "hello world", will be output in the terminal.
Since we use the Command Palette so often, it would be useful to change the keyboard shortcut to something more convenient. With F1 or Ctrl+Shift+P, I inevitably have to move my fingers off the home row to activate the palette. Something like Ctrl+S would be more useful. Feel free to use a different keyboard shortcut. Just replace Ctrl+S in the instructions below with your shortcut.
Let's open the user interface for keyboard shortcuts.
There are two ways to search for keyboard shortcuts. You can search by the name of the command or by shortcut: ex. Ctrl+Shift+P
Since I proposed using Ctrl+S, let's search for that shortcut to see if anything is bound to it already.
The results will show several shortcuts. The main one we have to worry about is 'File: Save' which shows a plain Ctrl+S in the Keybinding column.
One tip for searching is that if you include the double quotes when searching for a shortcut:
"Ctrl+S"
then only exact matches will be included. In our case, this will eliminate the extraneous search results. Note, this only works for keybindings and not commands.
Continuing, let's clear this Ctrl+S binding to make room for the Command Palette.
Now, let's set Ctrl+S to the Command Palette.
There seems to be a problem. None of the search results appears to correspond to opening the Command Palette.
We know that we can search for shortcuts and that one of the shorcuts for the Command Palette is Ctrl+Shift+P. Let's search for that shortcut.
Now we should see "Show All Commands" as one of the results. This appears to be the command for the Command Palette. Let's search for that command to see what turns up.
Now we see two results. We know the Command Palette has two keyboard shorcuts: Ctrl+Shift+P and F1. And we see both shortcuts represented in these results.
There is no reason to erase these shortcuts. Let's add a shortcut for Ctrl+S.
Because we left "Show All Commands" in the search box, the results will update to show the new keybinding.
Now we can press Ctrl+S to bring up the Command Palette.
Created: Thursday, August 19, 2021
Updated: Thursday, August 19, 2021