Converting a PowerShell (PS1) Script to an EXE File

PowerADM.com / PowerShell / Converting a PowerShell (PS1) Script to an EXE File

A separate *.ps1 extension is used for PowerShell script files. To run PS1 scripts, users must open a PowerShell console, cmd, or code editor (such as PowerShell ISE or VSCode). If you want your users to be able to easily run your PowerShell scripts, you can convert PS1 files to the .exe executable file format.

PowerShell scripts that are compiled into .exe files have the following benefits.

  • Allow users to run scripts in the usual way (by simply double-clicking an .EXE file) without opening a PowerShell console;
  • You can hide the PowerShell console from your users;
  • Users cannot edit the code of your PowerShell script;
  • PowerShell execution policies do not block EXE files from running on Windows;
  • Additional files can be added to an executable

There are several ways to convert PowerShell scripts to .EXE files, and we will look at them in this article.

Convert PS1 Script to EXE with PS2EXE

You can use the ps2exe tool to convert any PowerShell script into an executable EXE file. This console tool allows you to compile an executable from any PS1 script file.

You can install or download the ps2exe tool from the PowerShell Script Gallery https://www.powershellgallery.com/packages/ps2exe/1.0.12.

To install the ps2exe utility online from PSGallery on Windows, run the command:

Install-Module -Name ps2exe

You can now convert your PowerShell scripts to executable files by using the Invoke-ps2exe command. For example:

cd c:\PS
Invoke-ps2exe .\psscript.ps1 -outputFile .\psscript.exe

Invoke-ps2exe used to convert ps1 powershell script file to exe

The script compiles the executable from the specified PS1.

By default, ps2exe compiles your PowerShell script as a console application. If you need a Windows GUI application, add the noConsole option. Such an application will output all messages from the PowerShell console to a graphical dialog box.

test compiled exe file

The compiled exe file has a few additional options. For example, if run with the –wait option, the script will wait for the user’s response to complete.

A full list of all the options available for the ps2exe utility can be obtained as follows:

get-help Invoke-ps2exe

or by running the Invoke-ps2exe command with no parameters.

ps2exe command syntax

Win-PS2EXE is a graphical version of the ps2exe tool. Here you can specify the options required to compile the EXE from PS1 in the GUI.

Win-PS2EXE Graphical frontend to PS1-to-EXE convertor

Note that PowerShell scripts compiled in this way will run regardless of the PowerShell Execution Policy settings on the computer.

compiled powershell script bypass execution policy

When compiling scripts, ps2exe wraps your PowerShell script code in C# code. The resulting exe file is a .Net assembly containing the base64 encoded source script. For this .exe file to work, you must have PowerShell and the .Net Framework 2.0+ installed on your computer.

Do not store passwords, keys, and other sensitive information in such exe files. Any user can view the source code of your PowerShell script from the compiled .exe file by running:

.\psscript.exe -extract:"script_source.ps1"

How to Compile EXE from PS1 with IExpress?

You can use a built-in Windows IExpress 2.0 tool to convert a PS1 PowerShell script to a .exe file. Run the command:
iexpress.exe

run iexpress wizard windows

Then sequentially select:

  1. Create a new Self-Extraction Directive file;
  2. Extract files and run an installation command;
  3. Specify your package display name (for example, myPS1_to_exe);
  4. No prompt;
  5. Do not display a license;
  6.  Specify the path to your PS1 script file;add ps1 script to exe
  7. You can add additional files to the package with the script (configs, ini files, certificates, etc.);
  8. Specify the command to execute the script file:
    powershell.exe -ExecutionPolicy Bypass -File myposh1.ps1run powershell script from iexpress package
  9. Select the start mode (I use the Hidden mode);
  10. You can specify a message to be displayed after the script is run;
  11. Then specify the directory and name of the executable EXE file to be created. You can check the option Hide File Extracting Progress Animation from User;compile ps script to exe
  12. Select whether a reboot is required;
  13. The result is an executable file.

powershell script in exe file package
Paid PS1 to EXE conversion tools are available for development IDEs.
For example:

  • Powershell Pro Tools for VSCode
  • Powershell Studio
Leave a Reply

Your email address will not be published. Required fields are marked *