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
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.
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.
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.
Note that PowerShell scripts compiled in this way will run regardless of the PowerShell Execution Policy settings on the computer.
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.
.\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
Then sequentially select:
- Create a new Self-Extraction Directive file;
- Extract files and run an installation command;
- Specify your package display name (for example, myPS1_to_exe);
- No prompt;
- Do not display a license;
- Specify the path to your PS1 script file;
- You can add additional files to the package with the script (configs, ini files, certificates, etc.);
- Specify the command to execute the script file:
powershell.exe -ExecutionPolicy Bypass -File myposh1.ps1
- Select the start mode (I use the Hidden mode);
- You can specify a message to be displayed after the script is run;
- 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;
- Select whether a reboot is required;
- The result is an executable file.
Paid PS1 to EXE conversion tools are available for development IDEs.
For example:
- Powershell Pro Tools for VSCode
- Powershell Studio