PowerShell is now a lot more than a system management tool for Windows. The latest releases of PowerShell are built on the current cross-platform .NET and support macOS and Linux alongside the familiar Windows, making it a powerful management tool for all your systems. The underlying philosophy and language grammar remain the same, but the old PowerShell ISE development tooling is no longer under development and not supported for newer releases.
While the PowerShell ISE is still part of Windows, if you’re working with PowerShell 7, you’re going to need a development tool that works across all the supported platforms. Microsoft recommends using its Visual Studio Code editor along with the official PowerShell plugin.
SEE: Manage Active Directory with these 11 PowerShell scripts (TechRepublic Premium)
Code and PowerShell
Visual Studio Code has rapidly become one of Microsoft’s most useful developer tools. It’s a flexible, extensible editor for programmers that offers many of the features of an integrated development environment with the ease of a simple editor. One of its most useful features is a full-fledged extensibility model that lets developers build language extensions as well as debug and test tools into the editor. It’s also available on both Linux and macOS, so you can use it to build and test code targeting those platforms directly.
Among the many extensions are several that target PowerShell, including one from Microsoft. Like most new Microsoft developer projects, it’s an open-source package developed on GitHub. Microsoft’s Visual Studio Code’s PowerShell tooling is designed primarily to work with the PowerShell 7.2 long-term support release, though the older PowerShell 5.1 is still supported for backwards compatibility with existing scripts, so be sure to have a supported version installed before you start to use it.
Installing PowerShell and its Visual Studio Code extensions
Microsoft’s preferred installation method for PowerShell on Windows now uses its winget package manager, though you can download the current release installer from GitHub. Working with winget makes more sense, though. Once it’s installed, you can keep all your winget packages up to date with a single line of code, much like working with packages on a Debian- or Red Hat-based Linux distribution.
Installation is a little more complex outside of the Windows ecosystem. Linux users can install an appropriate package from GitHub or add a reference to Microsoft’s own repository to their list of trusted hosts, allowing you to use tools like apt or yum to install from the command line. macOS users can install from the Homebrew package manager or download a macOS package from GitHub.
Microsoft provides a simple way to install both Visual Studio Code and the PowerShell extension from the command line, using the PowerShellGet module. Type the following, and respond to any prompts:
Install-Script Install-VSCode -Scope CurrentUser; Install-VSCode.ps1
This downloads the installation script and then runs it as the current user.
If you already have PowerShell and Visual Studio Code installed, you can add the official PowerShell extension. There are many different PowerShell tools in the Visual Studio Marketplace, so be careful to install the official Microsoft extension. You might want to consider some of the others if you’re planning on using Visual Studio Code and its built-in terminal tools as your systems management environment. They include commercial tools that help add user interfaces to scripts as well as tooling for using PowerShell to work with cloud services like Azure from inside your editor.
Using the PowerShell extension
Visual Studio Code’s PowerShell extension works with the built-in language server to add support for PowerShell to your editor’s IntelliSense features. These will help reduce syntax errors, adding code completion and highlighting, make it easier to read code and add the correct cmdlets and variables to your scripts.
Usefully, there’s an option to choose the version of PowerShell you’re targeting, so you can switch between working with older and newer versions, and current and preview builds. This helps migrate scripts while maintaining compatibility.
It’s a good idea to use the most up-to-date version of Visual Studio Code possible. With newer versions, you can debug scripts that aren’t in your current workspace, which is a useful feature when dealing with downloaded scripts that you want to test before using in production.
Otherwise, Visual Studio Code will work in the context of an open folder or a Git repository, with the option of attaching its debugging tools to the console, so you can debug and test PowerShell commands before adding them to a more complex script.
Getting that old PowerShell ISE look
If you’re used to the look and feel of the original bundled PowerShell ISE, the Code extension contains an ISE Mode option. This isn’t the full ISE, but it goes some way to making Code a little bit more familiar, while still supporting its features. All you need to do is select Enable ISE Mode from the Code command palette, and it adds a command explorer view, while changing the layout and colors of the editing and terminal panes to match those of the ISE.
Microsoft provides a list of additional configuration options you can use to hide more of Code’s features if you want, but in practice, it’s best to stick with the defaults if you’re switching between editing PowerShell and any other language in the same project.
Remote editing and debugging using Visual Studio Code
Like the ISE, Visual Studio Code also provides remote editing tools, allowing you to work with PowerShell on a remote server from your desktop. Opening a remote connection is easy enough using the new Enter-PSSession cmdlet. This supports direct access to Windows machines by computer name or over Secure Shell via the hostname. The SSH option is a useful one, as it gives access to Linux and macOS hosts, including virtual machines running on Azure.
A built-in debugger launches and runs the current open script. You can add breakpoints, using all the familiar Code debugging commands to step through code. It will show the contents of any variables, with the option of watching specific variables.
Breakpoints can be set when a target function is called or if certain conditions are met, giving you more flexibility to work with interactive scripts. For example a conditional breakpoint will fire only when a set condition is evaluated as true, so your code can pass through a loop several times before triggering the break.
While Visual Studio Code is clearly the future for PowerShell development, it’s also working to bring the same level of integration to your choice of editor. PowerShell Editor Services is a .NET library and a JSON application programming interface that can be built directly into editors or into their own plug-ins. The tool offers the same features as the Visual Studio Code extension, so you will be able to get the same support for your code wherever you write it.
There’s a lot to like in Microsoft’s PowerShell tooling for Visual Studio Code. It builds on the familiar while taking advantage of the new, ensuring you can pick it up and start scripting with little or no learning curve and adding features and other extensions as you gain experience.
Master your Microsoft PowerShell knowledge with these online courses from TechRepublic Academy: