Getting setup for PowerShell Development
- PS1 – Should you learn PowerShell?
- Learn and use PowerShell with just three commands
- Working with the PowerShell Pipeline
- PowerShell History and Current State
- Getting setup for PowerShell Development
- Working With PowerShell Variables
- Taking Control with PowerShell Logic
- PowerShell Input & Output
- PowerShell Errors and Exceptions Handling
- PowerShell Remoting
Up to this point in the Learn PowerShell series we have written and run PowerShell commands directly in the PowerShell console. That quickly becomes impractical when you start writing larger sets of commands, or want to start sharing your scripts. Having an editor for PowerShell development makes writing PowerShell easier, and much quicker. This post will provide you with the setup and configuration needed to get started with PowerShell Development.
If you prefer video format over written documentation I discuss this topic in the following TechThoughts video:
Components and Configuration for PowerShell Development
PowerShell ISE no more
In the past the de-facto choice for writing PowerShell code was the PowerShell Integrated Scripting Environment (ISE). It is a native component that shipped with Windows PowerShell. With the release of PowerShell 6 and PowerShell becoming cross-platform, the PowerShell ISE is no longer supported. You can still use it (Windows only) to write PowerShell, but the future of PowerShell development is with Visual Studio Code (VSCode).
Components needed for PowerShell Development
1 – Install Visual Studio Code
VScode is a free, open source code editor that runs on Windows, macOS and Linux. It supports a variety of languages, intellisense, debugging, and is highly customizable with extensions. Download and install VScode.
2 – Install the PowerShell Extension
VSCode does not natively support PowerShell. To effectively write, debug, see syntax highlighting, use code snippets, and have intellisense for cmdlets, you’ll need to install the PowerShell extension.
- Select the extensions tab on the left side of VSCode
- Search for PowerShell
- Install the PowerShell Extension
With VSCode installed you can also utilize it from the terminal by typing code. Here is how you can install the PowerShell extension via a terminal:
code --list-extensions code --install-extension ms-vscode.PowerShell
3 – PowerShell Script Analyzer
The PowerShell Script Analyzer is a PowerShell module that acts as a static code checker. It will check your PowerShell code by running a set of rules to ensure you are adhering to best practices, and correct syntax. When you have both VSCode and the PowerShell Script Analyzer installed, VSCode will continuously check your code as you write it. Any errors will be displayed by visual queues in the editor.
In the above example the name of the function violates the PSUseApprovedVerbs rule. The example function is not using an approved PowerShell verb.
gci is violating the use of aliases rule. gci is an alias for Get-ChildItem. Aliases can introduce confusion into your code for others, so they should be avoided. Note that PSScriptAnalyzer is working with VSCode to highlight these two rule violations.
4 – Configure VSCode for PowerShell development
While the PowerShell extension does add a lot of capability, there are a few additional VSCode settings you’ll want to configure to enhance your PowerShell development experience. You can access VSCode settings with the keyboard shortcut: Ctrl + ,
VSCode has a graphical settings menu, but I’d encourage you to try using the JSON based settings file. The graphical menu is a recent addition to VSCode and the majority of online examples will include JSON examples. You can switch to the JSON version by clicking the Open Settings (JSON) button.
With the JSON settings file accessed, simply copy and paste the below into the file. These settings will provide a more streamlined PowerShell experience in VSCode. You’ll have tab completion, new files will open as PowerShell, and automatic script analysis will occur. Note that the location of the PowerShell path will vary based on if you are coding for PowerShell 5.1 vs 6 on Windows. It will also be different if you are developing on Linux. Review the sample below and adjust the appropriate lines to specify the version that you will be using for your terminal.
Don’t be intimated by configuring via JSON. It does make a certain logical sense that an efficient way to configure your code editor, is by using code. JSON is easy to read and format. Try hovering over a setting and you’ll notice that VSCode provides information about what that setting is for.
Save your settings.json file. You are now set up for efficient PowerShell Development using VSCode. Test things out by launching a new file either via File -> New File, or using Ctrl + N. With your settings changes, it should default to a PowerShell file. You can confirm this by looking in the lower right of VSCode:
Getting familiar with VSCode
As we continue in the Learn PowerShell series we will be using VSCode a lot. It’s worthwhile to spend some time exploring this versatile code editor.
You’ll likely be spending quite a bit of time looking at VSCode. Having a theme that’s easy on your eyes makes that a more enjoyable experience. There are thousands of themes to choose from in the Extensions tab. I prefer the Dark+ Material theme. If you miss the look of PowerShell ISE, there’s a theme for that! You can see the default available themes by opening the Command Palette.
The Command Palette is an important component of VSCode where you’ll perform many various actions. Open the Command Palette by either pushing F1 or Ctrl+Shift+P. If you are on Linux or macOS your VSCode keyboard shortcuts may be slightly different.
With the Command Palette open start typing Preferences:Color and select the Preferences: Color Theme option. This will display all available themes. Simply select your desired theme! If you can’t find one that suits you, install more via the Extensions menu.
Working with different file types
VSCode is extremely versatile. You can use it to work with a variety of file types, not just PowerShell files. With the settings previously established new files will default to PowerShell. Need to edit a markdown (.md) or json (.json) file instead? No problem, simply tell VSCode you want to work with a different file type.
You can accomplish this by clicking the file type in the bottom right corner. This will launch a Select Language Mode. Simply type the language type you like and select it!
Alternatively, you can quickly access the same menu with the shortcut Ctrl + K M to quickly change the file type.
Working with the terminal
When you write code in the editor, that code can be tested and run in the integrated terminal below. You can quickly toggle the terminal using the shortcut Ctrl + `
Stashing the terminal away allows you to see more of your code. With that shortcut you can quickly bring it back up when you want to test something or see terminal output.
To run PowerShell code in the integrated terminal you can place your cursor on a line of code and press F8. This will run that single line of code inside the terminal.
If you want to run multiple lines of code simply highlight all desired code and press F8.
Interested in even more VSCode configurations? Here’s my personal VSCode settings file that I use for daily PowerShell coding. Here’s a list of extensions that I like with code to quickly install them all.