avatarBlog

Summary

The web content provides a detailed guide on converting PowerShell scripts (.ps1) into executable files (.exe) using the PS2EXE tool from GitHub.

Abstract

The article outlines the process of transforming PowerShell scripts into standalone executables, enhancing their ease of use and accessibility. It introduces PS2EXE, a tool available on GitHub, as a straightforward and effective method for this conversion. The guide includes steps to install the PS2EXE module, set the execution policy to 'Unrestricted', import the module, and invoke the conversion process. It emphasizes the importance of changing the execution policy back to its original setting after the conversion to maintain system security. The article also touches on the additional option to hide the console window when launching a PowerShell GUI.

Opinions

  • The author suggests that converting .ps1 files to .exe simplifies execution, making it as easy as searching for the tool via the Start menu and pressing Enter.
  • The article expresses a positive view of PS2EXE, describing it as both simple and useful, and acknowledges the community effort by stating, "Good thing we get to stand on the shoulder’s of giants."
  • A disclaimer is provided to remind users that they are responsible for any potential malicious outcomes when changing the execution policy, indicating the author's caution regarding system security.
  • The author humorously addresses potential confusion among readers unfamiliar with PowerShell concepts, likening the process to overcoming obstacles in reverse order and reassuring them of its simplicity.
  • The conclusion encourages ethical use of PS2EXE, urging users to refrain from creating harmful programs and to adhere to the guidelines provided in the PS2EXE README.

Convert PowerShell (.ps1) to an Executable (.exe)

When we create PowerShell Scripts or Graphical User Interfaces (GUIs) there are different ways to execute the script. We could right click the file and choose “Run with PowerShell”, or we could doubleclick it as an executable. This makes it easy to find and quicker to launch from the start menu. Just press (Windows key, type the name of the tool, and press Enter) …et voila!

One of the best methods for your toolbox is to convert a .ps1 file to .exe

Good thing we get to stand on the shoulder’s of giants. PS2EXE is simple as it is useful. It can be found from a GitHub repository: https://github.com/MScholtes/PS2EXE

Here’s how we install it and turn a script into an app.

MScholtes PS2EXE Github
  1. Select the Code Dropdown button and Download the ZIP
  2. Unzip the file
  3. Open a PowerShell Admin console
PowerShell

INSTALL THE PS2EXE MODULE

Type the following command into PowerShell:

PS C:\> Install-Module -Name PS2EXE

Great job!

SET EXECUTION POLICY, IMPORT, and INVOKE PS2EXE

Now we need to run the Invoke-PS2EXE command to convert it into an executable, but the probability that this will run without a hitch is low until we import the ps2exe module.

I’m sure some of you are saying, “What is this gibberish?! Import the what in the where for what to do huh!?”

Basically, our worst case scenario is that we will have three roadblocks. The first one is to invoke PS2EXE, but we can’t do this until we import PS2EXE. And we cannot import until we set the execution policy to the correct setting. Simple, right? It’s actually easier than it might seem.

Let’s do a recap:

  1. Invoke
  2. Import
  3. Set the Execution Policy

But in order for the issue to be resolved, we will need to face these obstacles in reverse order: 3, 2, 1.

ENSURE THE EXECUTION POLICY IS SET TO UNRESTRICTED

Unless you like playing with fire, the odds are that your execution policy is set to something other than ‘Unrestricted’. It might be something like Allsigned, Default, RemoteSigned, or Restricted. Whatever it is, it’s best to check by typing the following:

PS C:\> Get-ExecutionPolicy

Remember this; we will revert back to it when we’re done.

Get-ExecutionPolicy

In my example it’s set to Restricted. Yours might say Allsigned, Default, Bypass, or something else. In order to convert your script into an executable, we will need to change your Execution Policy to Unrestricted. Trusting this GitHub source is necessary to move forward to convert your .ps1 to an executable. Disclaimer: I cannot be held liable if anything malicious happens to your computer. Just remember that we will be changing it to your original settings when we’re done.

Let’s type the following:

PS C:\> Set-ExecutionPolicy Unrestricted

Follow this command with Importing and Invoking in the next sections.

IMPORT PS2EXE

After the Execution Policy is set to allow anything to be imported, we’ll type the following:

PS C:\> Import-Module ps2exe

Awesome! Now, let’s move quickly.

INVOKE PS2EXE

Make sure that this next command includes the location AND name of the existing .ps1 file if you are not working in the same directory. Go ahead and type the following:

PS C:\> Invoke-PS2EXE -InputFile NameOfYourScript.ps1 -OutputFile NameOfYourExecutable.exe

As a kicker, if you’re launching a PowerShell GUI, then we can hide the console while the GUI launches. Just add-noConsole to the end of the Invoke-PS2EXE line like this:

PS C:\> Invoke-PS2EXE -InputFile NameOfYourScript.ps1 -OutputFile NameOfYourExecutable.exe -noConsole

FINAL STEPS

Don’t forget to change the Execution Policy back to your initial settings. Type the following and add Allsigned, Restricted, or whatever it was set to at the end. See the below example:

PS C:\> Set-ExecutionPolicy Restricted

CONCLUSION

Look in the folder you saved the executable, double-click to execute, or add it to your favorite stick. Cheers!

I urge you to take heed of the PS2EXE README and, **Please do not use PS2EXE to create harmful programs!**

Scripting
Windows
Ps2exe
Powershell Tutorials
Powershell
Recommended from ReadMedium