Latest posts by Michael Pietroforte (see all)
- Author and member of the year 2019 – Why DevOps still doesn't rule the IT world - Wed, Jan 1 2020
- Results of the 4sysops member and author competition in 2018 - Tue, Jan 8 2019
- Why Microsoft is using Windows customers as guinea pigs - Reply to Tim Warner - Tue, Dec 18 2018
Microsoft Visual Studio Code (VSCode) supposedly replaces PowerShell ISE and is perhaps the best free code editor for PowerShell. The nice thing about VSCode is that you can integrate any shell including different PowerShell versions, cmd.exe, and bash.
Why Visual Studio Code ^
Note that on Microsoft sites you'll often find the abbreviation "VS Code" instead of "VSCode" for Visual Code. But Google says VSCode is more common, so I'll stay with the recommendation of the #1 spelling expert on the planet.
You've probably heard the news that PowerShell Core 6 is out and that Windows PowerShell has no future. This also means that the PowerShell ISE that still ships with Windows 10 will soon be history. Thus, the earlier you move on, the better it will be for your career because you will stay ahead of the pack.
This doesn't necessarily mean you also have to move from PowerShell 5.1 to PowerShell 6, because VSCode works well with Windows PowerShell. Actually, VSCode has been available since 2015 and supports all common programming languages. You can run PowerShell 5.1 and PowerShell Core 6.0 on the same Windows computer and integrate both shells in VSCode. This is very useful if you want to test whether your old scripts also work in PowerShell Core.
A (long) while back I argued in a blog post (which became quite popular) that PowerShell ISE is a better terminal than the PowerShell console. I still don't like the PowerShell console because its interface lacks many features of modern terminals, and it still is essentially the same ol' MS-DOS console. Heck, the PowerShell console doesn't even support tabs.
A downside of PowerShell ISE as a terminal is that it sometimes behaves a bit differently than the PowerShell console does. Thus, if something doesn't work as expected, you always wonder whether ISE was the culprit. The good news is that this isn't to be expected with VSCode, because it just integrates the installed shells. For instance, if you install VSCode on Linux or macOS, you can also integrate bash, and on Windows you can work with cmd.exe (see below).
Install Visual Studio Code ^
To get started, download Visual Studio Code. And since you are already in the browser, you might as well download PowerShell 6 for all the operating systems you love. I will only cover Windows here, but I am also working with VSCode and PowerShell Core on a Mac, and it is essentially all the same. The only differences I've noticed so far are a few keyboard shortcuts are a bit different and the configuration files need to know the OS where VSCode runs on.
When you first launch VSCode, an error message will greet you:
Warn Git not found. Install it or configure it using the 'git path' setting.
Git installation not found
This is not a good a way to introduce a newly installed tool. Git is a version control system, and I think not every Windows admin will need it. But if you want a complete development environment, head over to the Git download page.
Don't make the mistake of opening the Git release notes with VSCode as the wizard suggests. This will cause trouble if Git hasn't initialized yet. When you restart VSCode after installing Git, the warning message will be gone.
You now already have a nice scripting environment for PowerShell. The Windows PowerShell 5.1 console is also already integrated. Click on the Terminal tab.
VSCode and PowerShell Core 6.0 ^
What is still missing is the PowerShell Core 6 integration. To integrate PowerShell 6, follow the menu path File menu > Preferences > Settings, add the line below to the user settings file (settings.json), and then hit Ctrl+S to save the settings.
"terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\6.0.1\\pwsh.exe",
Note that the exact location of pwsh.exe depends on your operating system and the PowerShell version. After you restart VSCode, PowerShell 6 is the new integrated terminal.
This is fine as long as PowerShell 6 is the only console you need, but I suspect you'll also want to work with other shells such as PowerShell 5.1 and cmd. As far as I know, there is no setting for this in VSCode. However, the really cool thing about VSCode is that many great extensions exist. Coincidentally, one such extension allows you to work with multiple shells in VSCode.
Multiple consoles in VSCode ^
To install Shell Launcher, click the extension icon in the left navigation bar and then type "shell launcher."
Next, open the user settings again to add the lines below:
"label": "PowerShell 5.1"
"shell": "C:\\Program Files\\PowerShell\\6.0.1\\pwsh.exe",
"label": "PowerShell 6.0"
"shell": "C:\\Program Files\\Git\\bin\\bash.exe",
"label": "Git bash"
You now have to click the Reload button.
Note that the exact locations may vary depending on your operating system and shell version. If you want, you can remove the line we added above to set PowerShell 6 as the default shell. That way, PowerShell 5.1 will be your default shell (not really the recommended option).
To launch one of the new integrated consoles, open the Command Palette in the View menu or press Ctrl+Shift+P. Then start typing "shell launcher" and select the extension. You should now see the new shells.
Select the shell you want to work with and hit Enter.
Once you start a new shell this way, it will stay open until you close VSCode. You can switch between shells in the Terminal tab as shown in the screenshot below.
You can also work with multiple consoles of the same shell by clicking the + icon.
You might feel always going through the Command Palette is too longwinded to start a new shell in the Terminal tab. If so, you can assign a keyboard shortcut to Shell Launcher. Navigate to the Keyboard Shortcuts (File > Preferences) and start typing "shell launcher." Then double-click the extension and assign your shortcut (for instance, Ctrl+Shift+T).
In my next post, I will show you how you can add more features to VSCode with the PowerShell extension and explain how you can conveniently execute code from the editor just like you're familiar with from PowerShell ISE.