Search This Blog

Sunday, September 18, 2022

PowerShell: Turning scripts into .exe

Let's say you need to make a portable script and bring it to another computer then for your basic scripts this could be the right solution for you. Using this way you can turn your .ps1 into a working .exe which then can be run like an ordinary file.

First of all, finish up your script. Make sure that file references are dynamic and that the right assemblies are loaded into the script. In a script that reads content for me I started the .ps1-file with the following lines:
 
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Speech
$Speaker = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
 
Once you feel like your script is ready, go to your start menu and launch "IExpress" as an administrator.


1. Choose "create new self extraction directive file" and press next. A SED-file is a file with the settings for your .exe file such as what to prompt the user, how to launch and where to fetch the dependencies.

2. Choose "extract files and run an installation command", and press next.

3. Package title is what name the installation dialog boxes will display. Choose a name and continue.

4. Confirmation prompt can be left empty. This basically asks the user if they want to install or not.

5. License agreement can be left empty as well.

6. Packaged files, choose the script and any files that it depends on.

 
 
7. Install program to launch, here you pick the program that will install/run your program.

Write: powershell.exe -executionpolicy bypass -windowstyle hidden -file filename.ps1

Leave the second box empty.

This will allow PowerShell to run without any execution policies blocking the installation. You will also avoid the big PowerShell window in the background by hiding it.

8. Show window should be set to default if you are using a GUI-application using Windows Forms for example.

9. Finished message, no need to set one unless you are running a script in the background.

10. Set the saving location, check both boxes and press next. If you need to run the .exe on Windows 95 then you don't check the second box.

11. Configure restart, select "no restart" and press next.

12. Save the self extraction directive if you want to go back and make changes in the setup of your .exe, if you don't save the file you just need to make the choices again should you want to remake the executable.

Done, now you should try run the exe and also try running it from different locations.