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 https://www.powershellgallery.com/packages/ps2exe/1.0.11.
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
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.
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.
Win-PS2EXE is a graphical version of the ps2exe utility. Here you can specify the required compiling options with a simple GUI form.
Note that PowerShell scripts compiled 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 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"