Running and debugging PowerShell Core scripts on macOS and Linux

Sébastien Bertrand
4 min readAug 15, 2018

--

PowerShell is Microsoft scripting language. Only present on Windows in the past, it is now open-source and available on Linux and macOS thanks to .Net Core.

In this article I’ll show you how to install PowerShell Core and use Visual Studio Code to edit, run and debug .ps1 scripts.

Installing PowerShell Core

There are two ways to get PowerShell up and running. You can either install it directly on your machine or execute it in a docker container.

Docker

For the docker setup, simply run:

docker run -it microsoft/powershell

macOS

To install and run PowerShell on macOS, execute the following commands in your terminal:

#Install PowerShell
brew cask install powershell
#Start PowerShell
pwsh
PowerShell Core running in a macOS terminal

Linux

PowerShell Core supports multiple Linux distributions (even Raspbian!). You can either download the package that matches yours directly on the PowerShell GitHub repository or install it using the command prompt (see the Microsoft documentation for more details about your distribution).

If you have issues installing it check out the PoweShell GitHub repository for more details about prerequisites.

Create, run and debug scripts using VS Code

Setting up Visual Studio Code

What would PowerShell be without a proper IDE to write, execute and debug scripts?

Fortunately there is a VS Code extension that does just that! You can either find it online or directly in the VS Code Extensions Marketplace

VS Code PowerShell extension

Once installed let’s write our script!

Script creation

First create a new folder and open it with VS Code. (Do not simply create a new file. An open folder is required in order to have access to the debug tools). Then create a new file with the “.ps1” extension.

Here is the script content:

Hello, PowerShell

It simply outputs “Hello, World!” in a txt file, reads that file and replaces a part of the initial text. It prompts the new text in the terminal and write it back in the file.

Debug configuration

To debug our script we will first need to create a new debug configuration. To do so go to the debug panel of Visual Studio Code and select “Add Configuration…”

Add a new PowerShell debug configuration

This will create a launch.json file that will tell VS Code how to execute our script when we press Start.

The newly created launch.json config file

Script execution

Back to our script, let’s add a few breakpoints and start the debug session.

Our PowerShell script running in debug

Visual Studio Code will start executing our script and will stop — as expected — at each of the defined breakpoints. You can see that it also provides a complete set of debugging tools as well as a PowerShell command prompt at the bottom.

Conclusion

Thanks to .Net Core we are able to get PowerShell up and running on macOS and Linux. These platforms being quite different from Windows, you probably won’t have a lot of scripts to re-use if you were mainly managing Windows servers. What’s the benefit you might ask then? Modules!

One of PowerShell strengths is its ability to add extra features thanks to additional modules. For instance, you can add a module to remotely manage your Azure environment.

The open-source community is also working on modules to extend the language. For more details about these you can have a look at the following resource: https://github.com/janikvonrotz/awesome-powershell

Do you have any additional tips? Remarks? Feedback? Do not hesitate to share your thoughts!

--

--