When working with PowerShell using Visual Studio Code it’ll default to the old PowerShell terminal. By using $PSVersionTable we can see it’s running PowerShell 5.1 but we’ll want 6.2. Also I wan’t to test using PowerShell Core on both Windows and Linux. To use .NET Core and PowerShell Core together and test cross platform development we’ll need to install PowerShell Core and the .NET Core SDK on Windows 10 and also within the Windows Subsystem for Linux (WSL).
PowerShell Core and .NET Core on Windows 10
- Download and install the latest stable release .msi for Windows (x64) from https://github.com/PowerShell/PowerShell
- Currently the latest version is PowerShell Core 6.2 https://github.com/PowerShell/PowerShell/releases/download/v6.2.0/PowerShell-6.2.0-win-x64.msi
- You can also find instructions for installing PowerShell on Windows here https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-6
- Download and install .NET Core 2.1 which is the current LTS version. While there’s also .NET Core 2.2 and a preview of 3.0 I intend to develop AWS Lambda for PowerShell functions, 2.1 is the latest version it supports. https://dotnet.microsoft.com/download/dotnet-core/2.1
- Install the Visual Studio Code extension for PowerShell
- Restart Visual Studio Code
- With the PowerShell extension installed we can use the integrated terminal for PowerShell Core 6
PowerShell Core and .NET Core on WSL
- Open the WSL. I’m going to use Bash on Ubuntu on Windows within the WSL. For information on that see my prior post. https://fitch.tech/2019/04/23/essential-cloud-development-tools-for-windows-10-users/
lsb_release -ato see the OS version. In my case that’s Ubuntu 16.04
- Install PowerShell Core on Linux, in this case within the WSL terminal https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-6
- Install the .NET Core 2.1 SDK https://dotnet.microsoft.com/download/linux-package-manager/ubuntu16-04/sdk-2.1.603
Switch to PowerShell 6 terminal in Visual Studio Code
A lot of post I’ve read about this said you can’t easily switch between shells within VS Code however that must have been for previous version or they didn’t know the trick. You need to open the PowerShell Integrated Console and not the default PowerShell terminal in VS Code. We can open the integrated console from the command pallet (Ctrl+Shift+P) then switch between PowerShell 5 and 6.
Pin the Terminal to the Right in Visual Studio Code
Personally I like having the Terminal pinned to the right instead of the bottom. I can see more of my code and run it in either the PS Integrated terminal or WSL terminal. You can even split them to view both for cross-platform PS development.
Close the Terminal panel. Then use Ctrl+Shift+P > PowerShell: Show Integrated Console to open it again. Then change the default terminal to WSL Bash. You can still open the PS Integrated console or create a new one from command pallet.
Then when you click on New Terminal (Ctrl+Shift+`) you’ll have a WSL Bash terminal. You can toggle between WSL Bash terminals and the PowerShell Integrated Terminal. Or if you select split view you can see both at the same time.
Now we can test our PowerShell functions within the Windows PowerShell Core 6 terminal and the Ubuntu WSL PWSH terminal. This will allow us to easily test for support on Linux. It’s important to understand that in PowerShell Core what works on Windows won’t necessarily work with PowerShell Core on Linux. On windows there’s some backwards comparability via the original .NET 2 standard framework whereas on Linux that’s unsupported. Let try this out.
Run this in both the PS Integrated terminal and WSL PWSH terminals. We get an error back in WSL telling us that “System.Management currently is only supported for Windows desktop applications.”
This tells me that I won’t be able to run this particular function on Linux, which also means I won’t be able to convert this particular function (which I wrote 5 years ago) to a AWS Lambda for PowerShell function. Which simply means I need to modernize this function for .NET Core 2.1. When I originally created this function .NET Core didn’t even exist. In future posts I’ll explore just how I might do this.