PowerShell GUI and Package

email me

PowerShell is definitely the scripting flavor of the week. Thus, I have ended up purchasing several PowerShell books, just to learn the ins and outs of the scripting language. Something I have noticed in 100% of the books…is what they are missing, which is GUI coding and how exactly to create finished EXE packages.

Well, because of this, I bought some scripting tools, you know, the ones that provide a full IDE. They are nice, and even allow you to build some GUI components. But, sadly, a complete package still could not be built. Why not? Hundreds of dollars spent…but you can’t do something as simple as code a PowerShell script and package it with some resource files.
{eye-roll}

So, as a workaround, I figured out how to code my GUI components in PowerShell, package my resource files, and create an EXE. The answer was to use to WinRAR. That’s right, the compression utility. Here is an example.

In this example, I wanted to create a simple tool for scanning and deleting all duplicate files on a computer. And, the tool needed GUI components, was to be coded in PowerShell, and had to be contained in a single EXE file.

First, I created a PowerShell form in my IDE. It has a main function button, access to a log, and an info button. But, as you can see, there is a background (a picture of the friend I was creating it for).

And, I also had an animation that pops up using HTA, which contains a background image and a gif animation.

Using WinRAR, you have the ability to add all files, including your compiled PowerShell script, and then save it as an EXE file. Main.exe is the PowerShell script. Delete_Dupes.exe is the name of the WinRAR EXE.

The magic happens by selecting what to do after the files are extracted. You put your compiled PowerShell file name here:

When you’re done, the EXE file should look like this:

Now, when you click the Delete_Dupes.exe, it extracts the files, and launches the PowerShell script! I will cover the details of the Delete_Dupes.exe in another post.

As a side note, you may wonder…but, what about that ugly icon? Can it be changed? Yes! Using Resource Hacker, you can easily replace the embedded WinRAR icon with one of your own.

Resource Hacker (just open the archive in here)

The updated icon

Notes

This isn’t the first time I have packaged PowerShell scripts into EXEs. If you happen to have a compiler tool, I use EXEScript, you can create a wrapper/launch file that jumps to your PowerShell script. This would permit you to have an EXE that contains all your files, while still being able to do most of your coding in PowerShell. This method offers a little more security and allows you to add/edit the EXE file properties. For example, this is a wrapper I used in EXEScript to jump to a PS script:

@echo on
title SCCM Installation
color 0b
cls
  
set CurDir=%CD%
set PSPath=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
set Timer=ping -n 2 127.0.0.1
 
%PSPath% -Command "Set-ExecutionPolicy RemoteSigned"
%PSPath% -file "%CurDir%\script.ps1"
%Timer%>nul
exit /b 0

 

Full Solution