5 Essential Features in VS Code for PowerShell
PowerShell is an amazing command-line environment, but it is so much more than that, too. It is a fully-featured shell environment as well as a scripting language. In many respects, PowerShell is more powerful than other shell environments like Bash. So, you might be interested in using PowerShell for your IT administration duties, especially because it is now open-source and compatible with Linux and Mac OS. If you are solely a Windows administrator, you may not have much of a choice but to use anything other than PowerShell. And if you are a Windows admin, then you'd greatly benefit from our Microsoft Windows Server training.
That means you need a tool to write these scripts. Visual Studio Code, or VS Code, is the perfect tool for writing PowerShell scripts. VS Code is a lightweight, but extremely extensible, code editor for Windows, Linux, and Mac OS.
Ready to start using VS Code? Keep reading to learn more about five essential features you'll find in VS Code for PowerShell. Once you've covered the basics, dive into this Visual Studio Code (VS Code) Essentials training to get started coding, debugging, running tasks, and more in VS Code.
A Overview of the Best VS Features for PowerShell [VIDEO]
In this video, Trevor Sullivan covers various VS Code PowerShell extension features. He begins by exploring the VS Code interface, specifically as it pertains to the PowerShell language. You'll learn about several visual display features that make working in PowerShell easier, as well as a few core functions that will increase your capabilities.
VS Feature #1: Language Mode
VS Code can work with a lot of different programming languages. It includes specific features for each of those programming languages, too. But how does VS Code know which language it needs to support at any given time?
That's what the language mode in VS Code is. The current language that VS Code is operating in will appear in the bottom, right corner of the VS Code information bar at the bottom of the editing screen.
VS Code will automatically detect which mode it needs to be in depending on what kind of file you have open, too. Typically, VS Code uses the file extension to detect which language mode it needs to be in. For example, if you open a markdown file (a file with a .md extension), VS Code should display markdown in that bottom right corner of the VS Code screen.
Language mode will enable certain features like syntax highlighting. Certain plugins will use the language mode in VS Code for specific tasks or configurations as well. For instance, a popular plugin called 'Prettier' will auto-format code. Different coding languages have different formatting requirements or best practices, though. So, Prettier uses the language mode in VS Code to auto-format code properly.
VS Feature #2: Syntax Highlighting
Language mode governs a lot of configurations in VS Code. One of those configurations is syntax highlighting. Syntax highlighting is a function where VS Code will highlight keywords for a programming language, variables, and so on.
Markdown is a perfect example for this next tidbit. That's because Markdown can include other languages like HTML. Markdown is a popular 'language' used for creating and formatting documents. It's meant to be simpler to use than HTML but more powerful than plain text. As such, markdown is commonly used for readme files or various CMS applications.
If VS Code's language mode is in markdown, certain things, like syntax highlighting, won't be available for HTML. So, how do you turn on those features for HTML? Easy enough! Click the language mode button in the bottom right corner of the VS Code screen to open the language mode selector. From there you can change the language mode from markdown to HTML.
VS Feature #3: Intellisense or Tab Completion
One of the best features of VS Code is Intellisense, otherwise known as tab completion for other code editors. Intellisense is a tool that will guess what command you are trying to use based on the language mode that VS Code is currently in. When typing in a command, like 'Get', VS Code will open a list of commands with 'Get' in them with the most used commands populating at the top of that suggestion list.
As you continue to type in a command, VS Code will narrow down that list of recommended code snippets. If VS Code recommends the correct code snippet that you want to use, you can press the 'tab' key on your keyboard to auto-complete the rest of that code. This is why Intellisense is also called tab completion in other applications.
Intellisense is more than an autocomplete tool, though. It is also quick for code snippets and commands, too. Each recommended code snippet displayed by Intellisense will also list any other pre-requisite information that the code snippet might need. For instance, if you are calling a function in PowerShell that requires an integer passed to it, VS Code will show that the function needs that integer in its auto-completion list.
Unfortunately, the PowerShell plugin for Intellisense in VS Code has a habit of hanging. It is easy to fix, though. The Intellisense plugin for PowerShell just needs to be restarted. If this happens to you, follow the steps below to restart that plugin:
Press F1 or control+shift+P on your keyboard to open the VS Code command palet
Search for 'PowerShell'
Run 'PowerShell: Restart Current Session'
VS Feature #4: Invoke PowerShell Debugger – F5 Key
Debugging PowerShell scripts in VS Code is easy. All you need to do is press F5 on your keyboard to run the current PowerShell script in view in the VS Code editor. This will automatically launch a PowerShell instance in the console area of VS Code and run your scripts. This is a useful way to test code in a sandbox before deploying it to a production environment.
RELATED: Here's Why You Should Make Your VS Code Portable
VS Feature #5: Invoke PowerShell Debugger – F8 Key
The built-in terminal and debugger in VS Code are excellent tools. Like other code editors, you can run and test code directly in VS Code before deploying it. You can also set breakpoints in code so you can see exactly what is going on in that specific section of code where a breakpoint is set. Did you know there is another way to test specific blocks of code without setting breakpoints, though?
It's easy to do. Simply highlight the line or section of code you want to run and then press F8 on your keyboard. By doing this, VS Code will only run that specific code that is highlighted in the code editor screen. This is a useful and fast way of testing blocks of code without having to jump through multiple breakpoints or run an entire section of code.
In this article, we discussed five essential features of VS Code for working with PowerShell. Those features are:
Language mode. VS Code uses language mode to configure itself for the programming language you are using. Other features, like syntax highlighting mentioned below, depend on language mode to operate properly.
Syntax highlighting. VS Code is capable of highlighting things like keywords in a programming language, variables, or code brackets. This makes reading and writing code much easier.
Intellisense. Intellisense can make coding much faster by predicting which bit of code you are trying to type as well as any prerequisites for using that code snippet.
Script debugging & line level debugging. By highlighting code in the code editor in VS code and pressing F8 on your keyboard, you can execute only the highlighted code to debug and test it without having to run the entire code base or use breakpoints. You can also run the entire script in a PowerShell console by pressing F5 on your keyboard inside of VS Code.
delivered to your inbox.