Christof VG

You don't need to come out of your comfort zone, if automation is in it!

Automatically convert a PowerShell script to an executable

Read time: 5 minutes

Introduction

In my previous post “PowerShell GUI with externally managed data“, we created a PowerShell GUI script that uses externally managed data. But wouldn’t it be great if the application was just an executable, which could be easily used by end-users, and that could be deployed using Mobile Device Management? Of course! In this blog post, I will guide you through the process of updating your pipeline to automatically convert the PowerShell script into an executable.

Supporting code

The code of the GUI project, including the conversion to an executable, can be found on the github repository.

PS2EXE Conversion tool

For the conversion from a PowerShell script to an executable, I use the tool PS2EXE. This tool was originally created by Ingo Karstein, but is currently maintained by Markus Scholtes.

Next to a conversion to an executable, the script is also capable of setting meta data in the executable, like a description, company, …

This tool is added to the repository and will be called by the build script.

Conversion step in the build process

The build step, that will convert the PowerShell script to an executable, needs to run a PowerShell script as described here:

1
2
3
4
5
6
7
8
9
10
11
12
13
.\PS2EXE\ps2exe.ps1 -inputFile '.\Bin\EndpointManager.ps1' `
-outputFile '.\Bin\EndPointManager.exe' `
-verbose `
-iconFile '.\Icon\favicon.ico' `
-company '<company name>' `
-product '<application name>' `
-title '<application name>' `
-description '<application description>' `
-copyright '<copyright message>' `
-version "1.0.$(Build.BuildNumber)" `
-noConsole `
-noOutput `
-noConfigFile

The script is very self-explanatory. We provide the script with an input file, which is the PowerShell GUI script. The output file is the executable that will be created.

The icon file parameter is used to change the default PowerShell logo of the application to a custom logo. Then, parameters follow that set the meta data of the file.

version

I recommend using semantic versioning with the build part set to the build number. This creates a traceable link between the application and the build version, making it easier to troubleshoot when needed.

Extra helpful parameters

Some extra parameters are passed to the script:

  • noConsole: No console will be showed when running the application
  • noOutput: No console output will be generated
  • noConfigFile: No extra manifest, specifying which .Net framework should be used, should be created

With these parameters set, an executable is created that is not different to the end-user than any other application. Double click the icon and a window pops up without any PowerShell window.

Pipeline

So we added the PS2EXE files to the repository and created a script to convert the PowerShell script to an executable. Now, it is time to run the script from the build pipeline.

To do so, add a PowerShell script task to the build pipeline and call the script you created:

meta data

With the meta data set, the properties look like this when showing the properties of the executable:

The icon is also set to the custom icon:

Conclusion

Adding a step to convert the PowerShell script to an executable automatically is very easy and not very much more than a oneliner.

Feel free to leave any comments below, or send me a message over twitter.