How to use PowerShell script modules... on Linux

Published on: 8 Mar 2018 by: Shane

Tux the Penguin reading a Powershell book

Octopus Deploy supports re-usable PowerShell script modules that can be included in deployments across all of your projects. When a script module is included in a project, it will automatically become available to PowerShell scripts that run on Windows. You can also run your favorite PowerShell script modules on Linux, but they are not automatically made available to your PowerShell on Linux scripts. While we plan to provide first class PowerShell on Linux support, here is the trick to get them working.

Review the PowerShell on Linux installation instructions for more information.

For this example we will use a script module called Say Hello with the following content:

function Say-Hello($name)
{
    Write-Output "Hello $name. Welcome to Octopus!"
}

For help creating a script module in Octopus follow the instructions in the documentation.

We will use a project called PowerShell modules on Linux that includes the Say Hello script module. Our deployment process consists of a Run a Script step that will invoke the Say-Hello method from PowerShell. Our naive bash script would look like this:

pwsh -Command "Say-Hello"

If we attempt to run this script we are met with an error message indicating that the script module has not been loaded:

The term 'Say-Hello' is not recognized as the name of a cmdlet, function, script file, or operable program.

The contents of the script module are available to the step in an Octopus variable called Octopus.Script.Module[Say Hello]. In order to use the script module in our bash script we will retrieve the contents, save them to a file and then import them in our PowerShell script. We'll save the module to the /tmp folder for simplicity - PowerShell's Import-Module expects an absolute path. Here is what the bash script looks like with the module imported:

say_hello_module=$(get_octopusvariable "Octopus.Script.Module[Say Hello]")
echo "$say_hello_module" > /tmp/SayHello.psm1
pwsh -noprofile -Command "&Import-Module /tmp/SayHello.psm1; Say-Hello World"

When we run the script, our module is imported and we see our expected output:

Hello World. Welcome to Octopus!

Now you can re-use your library of PowerShell script modules on Windows and Linux.


Octopus Deploy is used by thousands of developers across the globe, from small companies to large enterprises. Find out if it meets your deployment automation needs by taking advantage of our free 30-day trial. You can spin up an instance with just a few clicks!