How To Use PS2EXE To Convert PowerShell Scripts Into EXE Files

Learn about PS2EXE, a PowerShell utility that converts scripts into standalone applications.

Brien Posey

May 24, 2024

orange stylized gears
Alamy

In this tutorial, Brien Posey introduces one of his favorite PowerShell utilities, PS2EXE, which allows users to convert PowerShell scripts into executable applications.

Posey provides step-by-step instructions on installing PS2EXE, demonstrates its practical use, and highlights key features and security considerations. Additionally, he explains how to access a PS2EXE GUI frontend that simplifies the process for those who prefer a visual interface.

Transcript:

Brien Posey: Hello, greetings and welcome. I'm Brien Posey. In this video, I'm going to show you one of my favorite utilities for PowerShell. It's called PS2EXE. The idea is that you can take a PowerShell script and turn it into an executable application.

Install PS2EXE

As you can see, I have the GitHub page open for PS2.EXE. Now, you don't have to go to GitHub to download PS2EXE. You can install it directly from PowerShell. The way that you do that is by typing:


Install-Module ps2exe

The main reason I wanted to show you this GitHub page is simply because if you scroll down, you can see the full syntax. Now admittedly, the full syntax is quite a bit to take in, but you don't have to use all these parameters. PS2EXE is actually really easy to use.

How PS2EXE Works

Let's look at how this works.

I'm going to go ahead and close out my browser, and here I am in PowerShell. Now, I've already installed the PS2EXE module. So, what I want to do is just show you how PS2EXE works.

If I just type Get-Child Item and press enter, you can see that right now I've got a couple of files in this folder. The main file that I want to show you is Calc.ps1. This is a calculator script that I created late last year.

The other file is Calc.ico. This is just a calculator icon that I downloaded from one of the many sites that offers free icons. I honestly can't even remember which site I got this from.

We'll come back to that file a little bit later.

For right now, what I want to do is convert Calc.ps1 into an executable file. So, the way that I do that is by typing ps2exe calc.ps1. Then I type whatever I want to call the executable file. Because calc.exe is a Windows executable, I don't want to reuse that. Instead, I'm just going to call this mycalc.exe.


ps2exe calc.ps1 mycalc.exe

I'll press Enter, and just like that, the new executable file has been created.

I'll go ahead and switch over to my Command Prompt window. I'll type dir, and you can see the new file right here. If I run that file, you can see that it executes.

Incidentally, whenever you compile a PowerShell script into an executable file, you no longer have to worry about execution policies. Even if I were to set my execution policy to “restricted,” the calculator app would still run because now we're running it as an executable rather than as a PowerShell script.

So, let me go ahead and close this out.

Why You Shouldn’t Embed Passwords

One of the extremely important things to keep in mind whenever you compile a PowerShell script into an executable is that you shouldn't embed a password in the PowerShell script, particularly if we're talking about clear text. The reason for that is that you can reverse engineer an executable and extract the PowerShell code that was used to create it.

Let me show you how that works.

As you can see, I've got the Command Prompt open and I'm in my scripts folder. I'll type dir, and you can see that we created an executable file a moment ago called mycalc.exe. What I'm going to do is type mycalc.exe -extract:. Then I have to provide a file name. I'll just call this CalcCode.ps1.


mycalc.exe -extract: CalcCode.ps1

I'll press Enter. And if I type dir, you can see that I've got a brand-new PowerShell script called CalcCode.ps1 That wasn't there a moment ago. Now I’ll type:


type CalcCode.ps1

When I press enter, you can see the actual code that was used to create the executable. So, that's why I said you shouldn't embed a password in a PowerShell script if you're going to be compiling it into an executable. There is a way to reverse engineer that script, access the PowerShell code, and retrieve that clear-text password from the code.

Adding Attributes to the Executable File

Now, there are a couple of other things that I want to show you about PS2EXE. To do that, I'm going to switch back over to my PowerShell window.

What I want to do is create an executable that is a little bit more fully featured. Let me switch back to my Command Prompt for just one second and open File Explorer. I right-click on the executable and go to Properties and the Details tab. You'll notice that we don't have a whole lot of details here. Let's go ahead and change that.

I'm going to run a new command in PowerShell. I'm going to type ps2exe, then the name of my PowerShell script, which is calc.ps1. Then I need the name for an output file. I'm going to call this one mycalc2.exe. Then what I'm going to do is specify an icon file, so I'll type -IconFile and use that icon file that I showed you a moment ago. I’ll put that file in quotation marks. Then I’ll type -Title, and I’ll call this one “Poseys Calculator”. And let’s add a copyright, some like is -Copyright “Copyright (C) 2024, All Rights Reserved”. Let’s also add a version. I’ll type -Version “1.0”.


ps2exe calc.ps1 mycalc2.exe -IconFile “calc.ico” -Title “Poseys Calculator” -Copyright “Copyright (C) 2024, All Rights Reserved” -Version “1.0”

I’ll go ahead and press Enter. The output file has been created.

So, let's look at what we've done.

I'm going to switch back over to my Command Prompt window. I'll type dir, and here you see mycalc2.exe, which we just created. You'll notice that the file size is slightly larger than mycalc.exe even though we compiled the same PowerShell script. I'm going to type:


mycalc2.exe

I’ll press Enter, and we can see that the calculator still runs. I'll go ahead and close this out.

Then I'm going to go to File Explorer. Let's just take a look at this. I'm going to right-click on mycalc2.exe and go to Properties. You can see that the icon file that I assigned now appears as a file property, and you can even see the icon next to the executable. If I go to the Details tab, we can see that the file description is now Poseys Calculator. We can see that the file version is 1.0 and the product version is 1.0. There's the copyright that I created. So, we've added various attributes to the executable file that we've created.

The attributes that I showed you are just a small sample of what you can do using PS2EXE.

As a matter of fact, I’ll switch back over to PowerShell and type:


ps2exe -?

I’ll press enter. You can see the full syntax and that there are a huge number of parameters that are supported. That kind of goes back to what I showed you at the very beginning of the video with the GitHub page.

Simplifying PS2EXE With win-ps2exe

So, the big question I'm sure some of you are wondering right now is, “Is there any way to take advantage of all these parameters but simplify the syntax a bit?” Well, there actually is a shortcut that you can use. What you can do rather than dealing with all these parameters individually is type:


win-ps2exe

This wasn't always supported. This is something that is relatively new. I'll go ahead and press Enter. And so, what we have is a GUI frontend for PS2EXE. Now, this isn't something that I created. This is part of the PS2EXE program that I downloaded from GitHub. As you can see, you don't have to worry about any of the command-line parameters. You can simply fill in the source file, the target file, the icon file, and then any additional information that you want. You can specify a version, a file description, a copyright, and a product name. Then there are even some checkboxes that you can use. For example, if you wanted to bind administrative rights to the executable, there's a checkbox for that. You can also specify if you want the executable to run as a standard executable or if you want to create a multi-threaded executable, and you can choose the platform that you want to run that on.

When you're done, just click Compile, and the file that you've specified will be created.

So, that's just a quick introduction to how you can compile a PowerShell script into an executable. I'm Brien Posey. Thanks for watching.

About the Author(s)

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like