Compile PowerShell scripts to exe

Script compilation to executable files is one of the features we have added in PowerGUI Pro 3.0.

You would likely want to use compile a script when you want to share it in the enterprise environment and you are not sure whether the other person is comfortable running scripts or you simply don’t want risking someone modifying the code.

To compile a PowerShell script:

  1. Open the script in PowerGUI Pro Script Editor,
  2. On the Tools menu, click Compile Script,
  3. Then specify the name and path for the exe file you want to create:

PowerShell Compiled scripts

The additional options you get are:

  • Show or hide the PowerShell Console window when executing the script,
  • Automatically close or keep the console window (if you do want to show it),
  • Protect the script source code by obfuscating it using a password you specify,
  • Add other PowerShell scripts  to the exe (if you have script which your main script is using).

[UPDATE] If your script access parameters – so will the exe file it generates. So for an executable generated from a script like:

param ($MyParam1, $MyParam2) "MyParam1: $MyParam1""MyParam2: $MyParam2"

You may use a command line like:
c:\Generated.exe -Arguments -MyParam2 "Value2" -MyParam1 "Value1"

 

To try this feature, you can install a trial version of PowerGUI Pro from here.

(Screenshot taken from the original PowerGUI 3.0 announcement which lists a lot of other great features we shipped in that release)

45 Responses to “Compile PowerShell scripts to exe”


  1. 1 Klaus Graefensteiner August 22, 2011 at 2:13 pm

    I like this feature.
    An extremely cool enhancement would be allow for other payloads that get compiled into the executable. I was thinking about assemblies and native executables.
    I have a PowerShell script that uses cURL.exe that I need to distribute as one executable.

    Klaus

  2. 4 chrisjwarwick August 22, 2011 at 4:26 pm

    Excellent facility. I’ve had complications before trying to talk someone through starting PowerShell, setting execution policy etc etc – so this could simplify things in those cases.

    Of course, you can do this natively (and WPK, the pre-“ShowUI” code from James Brundage, let you produce exe’s from scripts quite easily) – but this is seamless.

    I assume you need PowerShell installed wherever the .exe is going to run – so I gues it could be a problem in some cases targetting legacy (e.g. XP) machines…

  3. 6 Jonas October 18, 2011 at 2:25 pm

    Is it possible to compile exe files with the basic version of Powergui?

  4. 8 Anonymous October 20, 2011 at 3:16 pm

    Is it possible (there’re any way(s)) to revert the code inside the exe back to Ps script?

  5. 9 Anonymous October 27, 2011 at 5:49 am

    a very nice feature.

    however you need to have all the cmdlets on the featured machine to i guess. it do not compile the cmdlets needed into the exe file (atleast not on my tests)
    so it is not only powershell.exe that is needed but also the cmdlets you want to use on the machine running the compiled file ?

    or am i doing something wrong here 🙂

  6. 11 Vineeth May 5, 2012 at 9:05 pm

    Reblogged this on VM.Blog. and commented:
    Good post on how to run PS scripts as “.exe”

  7. 12 Mark January 21, 2013 at 10:59 pm

    Is it possible to decompile the exe back to PS1 in order to view the source code?

  8. 14 Dirk February 11, 2013 at 2:18 pm

    Is it possible to add the Quest AD extension to this exe?

    • 15 Dmitry Sotnikov February 11, 2013 at 7:01 pm

      The cmdlets need to be installed on the target machine separately. If they are – you can obviously use them in the exe – just add the snapin in your code.

  9. 16 Geert April 4, 2013 at 1:58 pm

    If you compile your script to an exe this way, does the computer on which the file is executed need to have execution policy set to allow all?

    • 17 Dmitry Sotnikov April 4, 2013 at 6:48 pm

      I am actually no longer on the PowerGUI team – so I am not sure. You can give it a try – PowerGUI is now just a free download.

  10. 18 Anonymous April 4, 2013 at 8:39 pm

    Is there any way to have the .exe run from the x86 powergui. I am having a issue running the exe when trying to connect to a databse

    • 19 Dmitry Sotnikov April 6, 2013 at 5:10 am

      If you are on Windows x64 you should have both PowerGUI x64 and PowerGUI x86 installed. My understanding is that x86 PowerGUI will create x86 exe… However, I am not 100% sure. If this does not work – try asking in the forums at http://powergui.org

  11. 20 rome ii total war keygen July 19, 2013 at 5:25 am

    Hi, i think that i saw you visited my web site thus
    i came to “return the favor”.I’m attempting to find things to enhance my site!I suppose its ok to use some of your ideas!!

  12. 21 Ebasaranoglu October 21, 2013 at 8:58 pm

    Hello,
    I have 25 scripts and a main script that calls these scripts. I add all scripts for dependency to create an exe from main script. Exe is created succesfully but it doesn’t work fine (as script version). A loop occurs and exe doesn’t finish.
    Do you have any suggestion?
    Regards

  13. 23 Dylan December 5, 2013 at 4:34 pm

    Hi. When I run the exe, I need it to find the path that the code is being run from. I use the code:

    $leftFileDirectory = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
    However, when its ran through the packaged .exe it always finds the path:

    C:\temp\Quest Software\PowerGUI\15cc29fb-e118-4fdc-b247-1cd8cd0507a4\

    I assume its becuase the .exe software routes it through a PowerGUI specified location. Is there a fix for this at all.

    Thanks in advance

  14. 28 Jay December 30, 2013 at 3:02 pm

    As much as this post is helpful, it is still highly insecure even today.
    – The exe is a packed archive, if no password is set to secure the content one can use a tool like 7zip to read it as an archive and extract the ps1 script.
    – The script is exposed, at run time, after the password is read.
    It could be helpful, but it’s giving people a false sense of security which makes it even more insecure!

    • 29 Adam Sulik January 16, 2014 at 1:42 am

      I wrote a script. compiled it. Then opened up the exe with 7 zip. I don’t see my original script anywhere. I see the “text” file where it should be but it’s not plain text. I’d like a secure executable for something im working on, don’t want prying eyes looking at the source, if i can’t do that with this, I have another route I can go, but this is much simpler. Where can i see the source code when I open it up?

      • 30 Adam Sulik January 16, 2014 at 2:17 am

        Nevermind I extracted the text file with 7 zip, and low and behold there is my script. Dang it! oh well back to the going the long way around 😦 I was hoping this would be a super simple way to hardcode things users shouldn’t see in the script. Note to self, the easy way out never works! lol

  15. 31 Алексей Максимов October 9, 2014 at 9:18 pm

    Спасибо Дмитрий за информацию. Есть также другие инструменты компиляции скриптов (скриптовые), например скрипт PS2EXE. В результате его работы исполняемый файл получается более быстро запускаемый, чем от PowerGUI

    Безделушка для транслитерации на PowerShell скомпилированная в исполняемый файл.

  16. 32 Rossati Giovanni March 15, 2015 at 10:29 am

    Good feature, but the .exe start is slow.
    John Rossati

  17. 33 Anonymous April 7, 2015 at 5:14 am

    I installed PowerGUI 3.8. anf the compile script option is grayed, how can I enable it?

    thanks

  18. 34 ortal April 7, 2015 at 5:18 am

    I installed PowerGui 3.8, and the option “Complie Script” is grayed and I can’t press it, how can I enable it?

    thanks,
    Ortal

    • 35 Dmitry Sotnikov April 13, 2015 at 1:29 pm

      Is this the latest version and a clean install?

      I believe this feature used to require a commercial license but has later been moved to the free version.

      If this is not a clean install, try locating and deleting (or renaming) PowerGUI folder in your profile – PowerGUI will then recreate the default profile next time it starts.

      If nothing helps – try asking at http://powergui.org

  19. 36 Krishna July 26, 2016 at 10:26 pm

    In my power shell script(task_scheduler.ps1) when runs in a system, it creates a daily task in task scheduler. This daily task is to run another power shell file (action.ps1) in the system.

    Now I would like to convert task_scheduler.ps1 into an exe file. Since this file accesses action.ps1 file, I tried giving the same as “dependency” in Power GUI (while compiling script).

    Now, the task is getting scheduled. But the task is not running the action.ps1 file. As the file is not present in the specified path.

    How shall I get the path whether the dependency ps1 files get saved?

  20. 37 Mike December 8, 2016 at 8:00 am

    Hi!

    How do you call a dependency in your script?
    Let’s say I created a powershell script “Test.ps1” with the following code:
    $word = Get-Content Test.txt
    echo $word

    I go to ‘Tools’ –> ‘Compile Script’ –> Select ‘Test.txt’ in a dependency to this cript & save the exe file. However the exe ps1 script cannot access the dependency.

    Am I doing something wrong? Is there like a general PowerGUI Variable in which the dependenies can be called?

    I can’t seem to find any information on how to call / access the dependant script. Forums are not available anymore etc.

    Many thanks in advance for your assistance 🙂

    • 38 Dmitry Sotnikov December 8, 2016 at 9:25 am

      I don’t think this feature was ever implemented. So basically you need all dependencies to either be on the target machine or inside that script that you are compiling.

      • 39 Nick in Chicago February 8, 2017 at 9:53 pm

        Windows’ security protocols make it difficult to copy files to workstations. I have started to write self-installing section at the beginning of my PS1 files, that copy the EXE and all dependent files first then it tests for the file locations. First I make a new directory on the workstation (usually: C:\OEM). After that, I copy files and verify to the files to C:\OEM\. The only thing is to do the first install I have to run the original PS1 file as admin to get the files to copy from the called from a directory. If I run the EXE then the program tries to call $env:temp location and my dependent files are located on the server. I have tried 5 different variables for the $PSScriptRoot and none work. I even paused the EXE and tried to grab the path with:
        $ProcessName = ‘MyConvertedScript’
        (Get-Process -Name $ProcessName).path #note: no extension on name

        but then path is blank. If I open Task manager and click on properties I can see the path of the paused process. It is there I just don’t know how to get to it.

        Nick in Chicago

  21. 40 Bikash Biswal October 15, 2017 at 7:36 am

    Hi,

    Thanks for the beautiful post.

    I have the problem here, the converted exe file doesn’t run on machines where execution policy set to default or restricted.

    Do you have any option to run this exe on these specific conditions?

  22. 41 Anonymous April 12, 2022 at 1:40 am

    how can i select dependencies that are not in the same directory as the main script, but are in subdirectories?


  1. 1 List Building Script | How Do I Build A List? Trackback on September 25, 2011 at 7:08 pm
  2. 2 List Building Script | How Do I Build A List? Trackback on September 26, 2011 at 8:30 am
  3. 3 PowerGUI 3.1 Released! « Dmitry’s PowerBlog: PowerShell and beyond Trackback on October 14, 2011 at 1:49 pm
  4. 4 Summary of 2nd Dutch PowerShell User Group – DuPSUG meeting with additional resources | blog.bjornhouben.com Trackback on June 8, 2013 at 12:51 pm

Leave a comment




Legal

The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

August 2011
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031