© Adam Bertram 2020
A. BertramBuilding Better PowerShell Codehttps://doi.org/10.1007/978-1-4842-6388-4_3

3. Use Visual Studio Code

Adam Bertram1  
(1)
Evansville, IN, USA
 

After all, code is just text. You could get by opening up a Windows notepad screen and just start typing away. At some point though, you’re going to go nuts trying to find code buried in multiple files, wondering where you defined a function, and struggling to rename that variable in 100 different places. You need an IDE.

IDEs have been around for decades as a staple for every software developer out there. But IDEs have always been thought of as a developer-only tool. Those of us writing PowerShell don’t typically think of ourselves as “developers,” but that’s not true at all. If you write code, you are a developer and PowerShell is code.

Since you’re a developer, you need a tool that helps you manage all of your code and assists you to not only write code but also run tests, debug, and run code in your environment. An IDE allows you to perform most, if not all, of the functions necessary not only coding in of itself but all of the tasks around coding that you never have to leave a single tool.

Back when PowerShell was just a part of Windows, many of us chose to use the PowerShell Integrated Scripting Environment (ISE) tool. The ISE came with Windows and was a great tool at the time for writing and managing PowerShell scripts. It still is a good tool to use for beginner developers, but you will soon see its limitations once you require more from your IDE.

Nowadays, we have Visual Studio (VS) Code. Microsoft has publicly confirmed VS Code is the choice for PowerShell developers. VS Code is routinely updating and always getting new features. The PowerShell extension for VS Code is also under active development, meaning bugs are being fixed and features added regularly.

There are many reasons to use Visual Studio Code over the PowerShell ISE which include:
  • The PowerShell ISE is purposefully built for PowerShell and PowerShell only. Although not necessarily a bad thing, nowadays, it’s rare someone is only working in PowerShell. Especially for larger projects, there are always JSON, YAML, or maybe a C# application involved. You need a tool that supports all languages which VS Code does.

  • VS Code is cross-platform. If you write PowerShell code but prefer to be on a Mac or Linux, you can write code on your platform of choice and then execute it in the target environment. You aren’t tied down to development only in Windows.

  • VS Code has a huge extension marketplace. VS Code is just a foundation for extensions. You can find an extension to help you do just about anything from managing your Azure subscription, creating AWS CloudFormation templates, and helping you write better PowerShell code with linting.

  • VS Code has an extensive code styling abilities. Using VS Code, you can customize if you’d rather use tabs vs. spaces, if you want a new curly brace on the same line or a new line, code colors, and a whole lot more. VS Code has a much richer ability to customize it to your liking than the ISE does.

Overall, the PowerShell ISE will do just fine for beginners writing a little PowerShell here and there, but even if you’re a beginner, you might as well start on a tool that will be with you from beginner to expert.

Further Learning

Install the PowerShell Extension

If you’re going to write PowerShell code in VS Code, you must install and use the PowerShell extension. VS Code has a huge selection of available extensions to improve upon its functionality. For PowerShell, a required extension is the PowerShell from Microsoft.

The PowerShell extension provides improvements such as syntax highlighting, time-saving snippets, quick navigation of PowerShell scripts and modules, linting, and more. The first task you must do when installing VS Code is to install the PowerShell extension.

Further Learning

Integrate VS Code with Git

VS Code has native Git integration. You should be storing all of your code in some kind of version/source control anyway. If you’re using VS Code, you should take advantage of this integration. Using VS Code, you can make commits, manage branches, push and pull from remote Git repos, and more. If you’re scared of the Git command line, no worries because VS Code has GUI options for nearly every Git command you can think of.

Begin storing code in individual Git repositories (repos) and opening up Git repos as folders in VS Code. It will get you in the habit of storing scripts in version control by default. Since Git is so tightly integrated with VS Code, you’ll soon easily become accustomed to using version control as a daily habit.

Further Learning

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.108.54