Converting PowerShell (.ps1) script to Windows Executable (.EXE) on Ubuntu 22.04.2 LTS

cn flag

I have a PowerShell script file (.ps1) that I need to convert into a Windows Executable file (.EXE). Here is a sample PowerShell script:

# This is a sample PowerShell script

# Define a variable
$greeting = "Hello, World!"

# Display the greeting
Write-Output $greeting

I am currently working on Ubuntu 22.04.2 LTS. To install PowerShell on Ubuntu, I followed these steps and successfully installed PowerShell, allowing me to run PowerShell scripts on my Ubuntu OS:

Run the following commands to install the required dependencies:

sudo apt-get install mono-complete
sudo wget -q$(lsb_release -rs)/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

Update the package list:

sudo apt-get update

Install PowerShell:

sudo apt-get install -y powershell

Additionally, you can install PowerShell via Snap:

sudo snap install powershell --classic

Once PowerShell is installed, I installed PS2EXE through the PowerShell tool.

Install-Module -Name ps2exe

I am sure that PS2EXE should help me convert the PowerShell script to an executable file. To convert the PowerShell script (sample.ps1) to an executable file (sample.exe), I used the following command in PowerShell: ps2exe -OutputFile sample.exe sample.ps1

This process worked seamlessly on Windows OS, but on Ubuntu OS, I encountered the following error: Operation is not supported on this platform.

PowerShell 7.3.4

PS /home/vboxuser/Documents/ocs> ps2exe -OutputFile test.exe test.ps1

PS2EXE-GUI v0.5.0.22 by Ingo Karstein, reworked and GUI support by Markus Scholtes

PowerShell Desktop environment started...

Reading input file /home/vboxuser/Documents/ocs/test.ps1

Compiling file...

MethodInvocationException: /home/vboxuser/.local/share/powershell/Modules/ps2exe/1.0.6/ps2exe.ps1:2678

Line |

2678 |      $cr = $cop.CompileAssemblyFromSource($cp, $programFrame)

     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     | Exception calling "CompileAssemblyFromSource" with "2" argument(s): "Operation is not supported on this platform."

Invoke-ps2exe: Output file /home/vboxuser/Documents/ocs/test.exe not written
Artur Meinild avatar
vn flag
Why would you want to convert a Windows-native script to a Windows-native executable format using Ubuntu? Please explain your rationale.
Jaskaran Singh avatar
cn flag
Since my project is already functioning within my AWS environment, which is based on Ubuntu, I have a complete setup utilizing PHP Laravel. In this particular section of my project, I need to generate executable files (exe) from dynamic PowerShell (ps1) scripts at runtime. These exe files will then be downloaded onto my customers' machines. Opting for a Windows instance solely for this occasional task would incur additional costs. This task occurs only upon user request, specifically when they wish to download an exe file tailored to their system.
Jaskaran Singh avatar
cn flag
The Author of PS2EXE has addressed and solved a similar problem but I am not sure how to apply this solution: Maybe anybody can help me?
dj flag

What happens without the sudo? You shouldn't need admin rights to make a file if you're in a writeable directory

Jaskaran Singh avatar
cn flag
Thanks! @furicle I have updated my question now.

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.