We’re happy to announce the return of PowerShell 7 script packaging! You can now target PowerShell 7 as a packaging target with PowerShell Pro Tools for Visual Studio, VS Code, and with Merge-Script. We’re even introducing experimental Linux and Mac support. You’ll be able to run your scripts cross-platform!

To compile for PowerShell 7, you will need the .NET Core 3.1 SDK or later.

About PowerShell 7 Executables

The PowerShell Pro Tools PowerShell 7 host is a self-contained .NET Core application. This means that the target machine does not need the version of .NET Core installed. The executable will be significantly larger than executables packaged for Windows PowerShell. This is because the Windows PowerShell runtime and .NET Framework are already installed on the target machines.

PowerShell 7 executables will not affect the machine’s existing PowerShell environment and will not install PowerShell 7 globally.

The PowerShell code itself is not compiled directly. It is embedded in the executable and then executed internally.

Compiling a Script in Visual Studio

To compile a script for PowerShell 7 in Visual Studio, you will need to create a new PowerShell Project.

Next, you’ll want to right-click on the PowerShell project. First, enable packaging

.

Next, select PowerShell 7.0.1 as your PowerShell Version. When you select PowerShell 7 as your PowerShell target, the .NET Version will be set to netcoreapp31 automatically.

After selecting your PowerShell version, you will need to right-click on your project and click build. After the build completes, you will be able to grab the executable path from the Output pane. You can then run your executable.

As you can see when I run my executable, I am running PowerShell 7.

Compiling a Script in Visual Studio Code

To compile a script in Visual Studio Code, you will need to PowerShell Pro Tools extension installed.

Open the script that you wish to package into the compiled executable. Click the package button on the top right corner of the VS Code action bar.

It will run a compilation with default settings but it will also output a package.psd1 file that you can then edit so that it compiles for PowerShell 7.

Set the PowerShellVesion setting to 7.0.0 and the DotNetVersion setting to netcoreapp31.

Now, navigate back to the script you wish to package and click the package button again. This time, the script will be packaged using the PowerShell 7 runtime. You can see the path to the executable in the terminal.

Compiling a Script with the Merge-Script PowerShell Cmdlet

The VS Code extension uses the Merge-Script cmdlet internally. You can use the same package.psd1 file that you created in Visual Studio Code directly with the Merge-Script cmdlet. The following command line will compile the script.ps1 into a PowerShell 7 executable.

Linux and Mac OSX Support (Experimental)

In addition to being able to compile scripts for Windows machines to run PowerShell 7 executables, you can also compile to target Linux. This is an experimental feature. We do not guarentee that everything will work. To compile for Linux of Mac, you need to set the RuntimeIdentifier property of the package config in package.psd1 file. This is currently only available in VS Code and using Merge-Script directly.

Here’s an example of compile a script for Linux.

After executing this command, you will have a file without an extension created in the output directory.

This is the executable that you can now run on Linux. You’ll need to ensure that you run chmod +x on the executable before executing it.

Conclusion

With the new version of PowerShell Pro Tools, you can now compile for PowerShell 7. This provides the ability to create stand-alone applications that run on Windows, Linux, and Mac. Give PowerShell Pro Tools a shot today.

See it in Action