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

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

You can install or download the ps2exe tool from the PowerShell Script Gallery

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

Install-Module -Name ps2exe

Now you can use the Invoke-ps2exe command to convert your PowerShell scripts into executables. 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 will generate a compiled Exe file for you.

By default, ps2exe compiles your PowerShell script as a console application. If you need a Windows application, add the noConsole key. 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, when run with the -wait option, the script will wait for the user’s response to complete.

A complete list of all available options for the ps2exe tool 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 utility. Here you can specify the required compiling options with a simple GUI form.

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

Note that PowerShell scripts compiled 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 the exe file to work, PowerShell and .Net Framework 2.0+ must be installed on the computer.

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

.\psscript.exe -extract:"script_source.ps1"
Leave a Reply

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