Testing PS Core Cross-Platform Support using VS Code with WSL PWSH and Windows PS Terminals

Standard

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

PowerShell 6.2 Terminal with .NET 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

Visual Studio Code extension for PowerShell

PowerShell Core and .NET Core on WSL

After installation of .NET Core 2.1 and PowerShell Core 6.1 on Ubuntu 16.04 WSL

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.

Ctrl+Shift+P to open the command pallet. Search for > PowerShell: Show Integrated Console

Ctrl+Shift+P again and search for > PowerShell: Show Session Menu

Select switch to PowerShell Core 6 (x64)

Click yes to the message asking to restart the terminal session.

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.

File > Preferences > Settings > WorkBench > Panel: Default Location > right

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.

Ctrl+Shift+P > Terminal: Select Default Shell

Select WSL Bash

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.

Click Split Terminal (Ctrl+\ ) to view WSL and PS terminals. Run PWSH in the WSL terminal to run PowerShell Core on WSL. Run $PSVersion table in both.

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.

[System.Management.ManagementObjectSearcher]""

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.”

“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.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.