Whether you're a developer or an it pro, you definitely have to deal with PowerShell sometimes.
For several year I used to develop PowerShell script into PowerShell ISE.
It worked very well, but it looked like "old".
Since many years, almost four, I started using Visual Studio Code to edit every kind of files, including PowerShell files.
The reason is really simple: Visual Studio Code is more powerful than PowerShell IDE.
I show you the reason why.
Table of Contents
- Platform support
- Installing the extension
- Choose the version of PowerShell for your session
- Example Scripts
- A specific configuration for PowerShell files
- How to debug PowerShell Script
- Command Explorer and Module Explorer
- PowerShell ISE Mode inside Visual Studio Code
- GitHub Copilot
- Code Snippets for PowerShell
- PowerShell Like a Pro Extensions Pack
- Conclusion
Platform support
PowerShell Core is supported by Linux, OSx and Windows and you can use Visual Studio Code extension for PowerShell in all of these releases.
On Windows you can also PowerShell v3 and higher.
To install PowerShell Core on these platforms, you can follow the installation instructions directly from the Microsoft website:
- Installing PowerShell Core on Linux
- Installing PowerShell Core on macOS
- Installing PowerShell Core on Windows
Installing the extension
Launch Visual Studio Code and press Ctrl+P (Cmd+P on Mac).
The editor opens a side bar Extensions with the PowerShell extension selected.
Click on Install and then Reload.
Choose the version of PowerShell for your session
You can install on your machine PowerShell Core side-by-side with PowerShell Vx.
If you have to choose a particular version for your script, you can open the command palette (Ctrl+Shift+P or Cmd+Shift+P on Mac) and write PowerShell: Show Session Menu.
Now you can choose the right version of PowerShell from the dropdown.
You can also select the PowerShell version from the green PowerShell icon on the bottom right of your editor.
If you click on it, the same command pallet appears on the top bar.
Example Scripts
Now you are ready to play and work with PowerShell and Visual Studio Code.
If you want to try them with some example scripts, you can click on the menu File->Open Folder or hit Ctrl+K Ctrl+O quickly and navigate to this folder:
C:\Users\<USERNAME>\.vscode\extensions\ms-vscode.PowerShell-<VERSION>\examples
You can open Code directly from a PowerShell terminal typing this command:
code (Get-ChildItem $Home\.vscode\extensions\ms-vscode.PowerShell-*\examples)[-1]
A specific configuration for PowerShell files
In the command palette (Ctrl+Shift+P or Cmd+Shift+P on Mac) write Open Settings (JSON).
Now you can see your preferences without the pretty UI.
Here you can add some additional settings specific for PowerShell.
"powershell.integratedConsole.focusConsoleOnExecute": false,
"powershell.integratedConsole.showOnStartup": false,
"[powershell]": {
"editor.renderWhitespace": "all",
"editor.renderControlCharacters": true,
"files.trimTrailingWhitespace": true,
"files.encoding": "utf8bom",
"files.autoGuessEncoding": true,
"editor.tabCompletion": "on"
}
Because you have added the language name, the other languages are not affected with these behaviors.
How to debug PowerShell Script
Since the version 1.9 you can debug single file without open the containing folder of the script.
So, you can open a .ps1 file in VS Code, set a break point (with the click on bar with the mouse or pressing F9 to the line of code) and press F5 to start a debug session.
Quick and easy.
There is another way to debug PowerShell files that add some additional features to the debug experience.
For example you can choose if you want to debug the script in an interactive session or into a PowerShell host process running on your PC.
You can create a debug configuration file following these simple steps:
- Open a folder containing your scripts
- Open the debug side bar and click on the gear icon on the top right.
- Select the Environment called "PowerShell", of course.
Now Visual Studio Code creates a file in your directory called launch.json with your debugging settings.
You can replace the content with this JSON:
{
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Launch (current file)",
"script": "${file}",
"args": [],
"cwd": "${file}"
},
{
"type": "PowerShell",
"request": "attach",
"name": "PowerShell Attach to Host Process",
"processId": "${command.PickPSHostProcess}",
"runspaceId": 1
},
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Interactive Session",
"cwd": "${workspaceRoot}"
}
]
}
You can find additional information about debugging to the following links:
- Debugging PowerShell script in Visual Studio Code – Part 1
- Debugging PowerShell script in Visual Studio Code – Part 2
Command Explorer and Module Explorer
If you like PowerShell ISE, you like the Command Explorer bar, for sure.
With the PowerShell extension for VS Code you have the same experience opening the command palette (Ctrl+Shift+P or Cmd+Shift+P on Mac) and typing "PowerShell Command Explorer".
There isn't a "native" Module Explorer in Visual Studio Code, but you can install another extension called "PowerShell Pro Tools" with this feature.
You need a licence key for it, but the Module Explorer is free.
PowerShell ISE Mode inside Visual Studio Code
If you are a big fan of PowerShell ISE, you can have the same user experience but with the power of Visual Studio Code.
To enable the PowerShell ISE Mode, you can open the command palette (Ctrl+Shift+P or Cmd+Shift+P on Mac) and type "ISE Mode". Select "PowerShell: Enable ISE Mode".
Now your Visual Studio Code looks like in the picture below.
GitHub Copilot
GitHub Copilot is a tool for developer, but if you are writing PowerShell Script, you are a developer, somehow.
If you want to write code faster, focus only on your logic and do your job, you can install GitHub Copilot extension for Visual Studio Code.
From the "Extensions" panel, search for "GitHub Copilot" and install it.
Pay attention because you can use Copilot only with an active subscription to the service.
It's not really expensive, especially for the added value it gives and the time it saves when writing code.
Code Snippets for PowerShell
To be more productive, you have to write more code in less time (and not only this).
Visual Studio Code supports code snippets and it's very easy to add your snippets specific for a language.
I have created a snippet Visual Studio Code extension and I have published it on the marketplace.
You can find the extension at this link hhttps://marketplace.visualstudio.com/items?itemName=emanuelebartolesi.powershell-snippets-vscode or from the Extensions panel you can search for "PowerShell Snippets for VS Code".
PowerShell Like a Pro Extensions Pack
I created an extensions pack for Visual Studio Code with all the extensions I recommend for PowerShell developers.
You can find the extensions pack at this link https://marketplace.visualstudio.com/items?itemName=emanuelebartolesi.powershell-like-a-pro or from Extensions panel you can search for PowerShell Like a Pro.
Conclusion
I hope I helped you to configure Visual Studio Code for a better experience with PowerShell.
Leave a comment if you have some interesting configuration for PowerShell!
Thanks for reading this post, I hope you found it interesting!
Feel free to follow me to get notified when new articles are out 🙂
Top comments (5)
Hi,
I just starting PowerShell development using VSCode coming from a PowerShell ISE and the feature I am missing the most is the ability to view currently defined variables and their values WITHOUT having to start a debug session and using break points.
Ideally the debug pane is visible showing local variables in the current PowerShell session and I can execute code from my ps1 file using F8 or write my code directly in the PowerShell terminal to e.g. modify variables
Is this possible at all?
kind regards,
-gerhard
There is a new capabilities in VS Code.
thomasmaurer.ch/2020/03/how-to-use...
Check it!
I have to update this post! :)
thanks for the reply!
will check out the linked post
Thanks for a nice article. I was however confused by the description here:
Launch Visual Studio Code and press Ctrl+P (Cmd+P on Mac).
The editor opens a side bar Extensions with the PowerShell extension selected.
Click on Install and then Reload.
When I press Ctrl-P I get a small dropdown menu (picture) - and not the Extensions sidebar. The extension was installed previosly, so this might explain the difference - but if I try to install a different extension, I see no Reload button.
I'm using version 1.38.1 on Windows 10.
great article!
10x 😃