- Use Azure Bastion as a jump host for RDP and SSH - Tue, Apr 18 2023
- Azure Virtual Desktop: Getting started - Fri, Apr 14 2023
- Understanding Azure service accounts - Fri, Mar 31 2023
You may have heard the news that Microsoft is no longer actively developing the PowerShell Integrated Scripting Environment (ISE). This means we Windows systems administrators need a new PowerShell code editor, and for most people, that road leads to the free, cross-platform, open-source Visual Studio Code (VSCode). Yes, "open source" means you can actually contribute to the VSCode code base if you are so inclined.
Today I'm going to teach you to configure VSCode so it behaves largely like the ISE. Honestly, I've found after much resistance that VSCode offers much more customizability (and a better debugger) than the ISE, so I'm happy I finally made the switch.
Install VSCode
Go ahead and install the latest VSCode release in any way you're most comfortable with. I suggest you also install Git so you can take advantage of VSCode's built-in Git version control support.
Note: For production environments, I suggest you use stable releases and not the Insiders edition. As it is, the VSCode development team commits stable releases at least twice a month, so you can expect frequent changes and fast bug fixes. Also, you can now install the software without administrative elevation.
Another tip I have for you is to include VSCode to your system's PATH environment variable. This will allow you to launch VSCode by typing Code from the Run dialog box or console prompt.
After installation, open the Run dialog box and type code script.ps1. You should observe that Visual Studio opens and creates a new file named script.ps1. At this point, Visual Studio code doesn't have PowerShell intelligence. However, we are going to fix that problem right now.
Install key extensions
Click the Extensions icon in the VSCode Activity Bar and type powershell to search the Visual Studio Marketplace. The Activity Bar and the various Side Bar panes are reminiscent of the ISE Add-ons pane.
Select Microsoft's PowerShell extension and then click Install. This extension unlocks PowerShell language support in VSCode, including IntelliSense code completion and a rich debugger. Click Reload to refresh your VSCode session after extension installation.
You will find that VSCode extensions update themselves periodically and that most extension authors work on a DevOps continuous integration software development lifecycle. This is twenty-first century IT after all!
We now have the PowerShell integrated console and full PowerShell support. Go ahead and type some PowerShell code in your script.ps1 file and test it out! By the way, save your script.ps1 test file to disk so we can continue using it throughout this tutorial. Let me walk you through the following annotated screenshot:
- A: Use the Tab and arrow keys to navigate IntelliSense
- B: Press F8 to run the current code line in the integrated Terminal; select Terminal > Run Active File to run the entire script file
- C: Access PowerShell extension options
- D: Change the PowerShell engine VSCode uses (yes, this works with PowerShell Core!)
Configure PowerShell settings
The Command Palette is the primary navigational/configuration tool in VSCode. Select View > Command Palette or press Ctrl+Shift+P, type configure, arrow down to Configure 'PowerShell' language based settings, and press Enter. As I mentioned previously, you can also click the PowerShell status bar entry to bring up the PowerShell extension options.
Note: The status bar displays the language VSCode identifies in your source code. For PowerShell script and module files, the PowerShell extension should display accordingly.
VSCode has a new user interface for navigating settings. As you can see in the following screenshot, PowerShell-specific editor settings are under Extension > PowerShell Config..., and you can find general editor settings under Commonly used.
We don't have the space to describe every setting, but I will link you to the VSCode settings reference and share some of my preferred editor/PowerShell settings:
"editor.mouseWheelZoom": true, "editor.minimap.enabled": false, "editor.renderWhitespace": "all", "editor.renderControlCharacters": true, "editor.wordWrap": "on", "editor.formatOnType": true, "editor.formatOnPaste": true, "files.trimTrailingWhitespace": true, "files.autoSave": "afterDelay", "powershell.enableProfileLoading": true, "powershell.integratedConsole.focusConsoleOnExecute": true, "powershell.integratedConsole.showOnStartup": false, "terminal.integrated.fontFamily": "Consolas", "terminal.integrated.fontSize": 18, "terminal.integrated.lineHeight": 1, "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "powershell.powerShellExePath": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "files.associations": {"*.ps1": "powershell"}
If you want to work with the raw settings.json user settings file, open the Command Palette and type Preferences: Open Settings (JSON).
NOTE: All your VSCode settings files are stored in the path %UserProfile%\AppData\Roaming\Code\User.
Change the theme and add icons
Microsoft packages the ISE color theme with the PowerShell extension. In the Command Palette, type theme to reach Preferences: Color Theme and then select PowerShell ISE from the list. Arrow up and down through the list of installed themes to test them out; press Enter to commit your change. Use the Extensions sidebar to search for more light and dark themes at your leisure.
PowerShell ISE color theme
I also suggest you install an icon pack to make your files look more meaningful. This step is always optional and perhaps a bit less important now that the ISE theme includes some basic icons. Three popular third-party icon packs are:
Below you can see the difference between the default VSCode icon view (left) and the Material Icon Theme view (right).
By the way, until you get the hang of the Command Palette, try the gear icon in the lower left corner of the interface. You can get to the icon option here too.
Install PowerShell snippets
Pre-built code snippets were one of my favorite PowerShell ISE features. Here's how you can add some nifty PowerShell code starters to VSCode:
- Copy the contents of Keith Hill's PowerShell snippets file from his GitHub repository.
- In the VSCode Command Palette, type snippet, then Preferences: Configure User Snippets, and then PowerShell.
- Paste Keith's snippets over the boilerplate content in the json config file.
As you can see in the next screenshot, AutoComplete and IntelliSense will show you snippet triggers as you type. Alternatively, you can use the Command Palette and type insert snippet.
Next steps
I hope you feel a bit more comfortable using Visual Studio Code. As I stated earlier, I resisted moving away from the ISE because it's been such a faithful tool to me over the years. However, the excellent, helpful community surrounding VSCode helped me to overcome my learning curve and enjoy using the tool to create and maintain my PowerShell code.
Let me leave you with some additional tutorial and reference links for your studying pleasure:
Subscribe to 4sysops newsletter!
- Visual Studio Code Tips and Tricks
- Using the VSCode Debugger
- Using Git with VSCode
- Customize VSCode Key Bindings
- Building Extensions for VSCode
Great article, thank you. Looks like I may be switching as well.
I had to click on RAW in Keith Hill’s GitHub repository or it wouldn’t copy properly.
Thanks for the article. I have been looking for something like it.
Allan
Thank you for doing an article like this.
However, it does not address the primary reason why I have not switched from ISE to VS Code: the ISE Command add-on (View | Show Commands Add-on).
I rely heavily on the ISE Command Add-on to help me search and filter commands from modules (either searching all modules or selecting individual modules from the dropdown, then searching).
Are you aware of any efforts to get this functionality similar to ISE’s Command Add-on into VS Code?
Hi John. It’s important to remember that VSCode tries hard not to be a GUI tool. Thus, you may want to get familiar with PowerShell commands like:
Get-Command -Module <module-name>
Get-Command -Verb Get -Noun AzureRMCompute*
That said, check the VSCode extension gallery to see if someone else developed a tool to address your use case. Thanks for reading! -Tim
Has anyone found a good (in depth) article on debugging PowerShell in VSCode? I find myself constantly writing in VSCode, and debugging in ISE w/ISESteroids.. (hopefully this isn’t hijacking this article/thread 🙂
David F.
Thanks, i would like to start using vscode but lack of time/will stick me to ISE, this is just what i wanted
Hi Timothy,
Thanks for you post.
On my side I have another reason to not switch completely to VSCode: the lack of PSRemoting tabs functionality.
With ISE :
(in VSCode your have to rename them manually)
(in VSCode you have only one “PowerShell Integrate” terminal which is linked to all the scripting tabs)
There is a VSCode extension named Terminal Tabs, but it’s just a look like, good for demos or labs. In fact it’s not doing the job of ISE remoting tabs at all.
That’s why I keep ISE for my SysAdmin work with multitasking, interactive sessions or short one-shot scripts while using VSCode for when I want to focus on developing modules or permanent scripts.
Hi
Good post. I think the PowerShell 1.11.0: or the (beta) PowerShell Preview 2.0.0 (ms-vscode.powershell-previewPreview) also support the snippets now.
Out of interest, I wonder if there is any resources covering some the settings mentioned
“files.autoSave”: “afterDelay”,
“powershell.enableProfileLoading”: true,
“powershell.integratedConsole.focusConsoleOnExecute”: true,
“powershell.integratedConsole.showOnStartup”: false,
@daniel
There are so many options available in VSCode…
Thanks for highlighting those ones!
I will try them right now.
For years now I have been using ISESteroids by Tobias Weltner which takes the ISE to another level. I hope that some of his work can be migrated across to VS Code. Until then I’m sticking with the ISE. Sometimes progress leads to unacceptable compromises and the benefits of using all the features available in one OS are missed in making it cross platform
I having also being using Tobias’s excellent ISESteroids for client work for a few years now so I understand your reluctance. Indeed the debugger is much more responsive than VSCode. That said, the writing is on the wall for both ISE and ISE Steroids so I am making the jump now.
Is it possible to automatically sort variables by name?
Hi Ian, Not sure if there is any IDE utility/tool to sort the variables.
However, such a requirement can be fulfilled with PowerShell.
get-variable | sort-object -Property Name
Do check out below article on this subject.
https://4sysops.com/archives/display-and-search-all-variables-of-a-powershell-script-with-get-variable/
What is the difference between pressing CTRL-F5 (DEBUG > run without debugging ) and clicking Terminal > Run Active File ?
the following command takes less than a second in powershell ise but takes 63 seconds in vscode. (substituting “domain.com” here for an actual internal domain)
Get-DnsServerResourceRecord -RRType A -ComputerName domain.com -ZoneName domain.com | Where-Object {$_.recorddata.ipv4address -like $ip} | Select-Object -ExpandProperty hostname