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)

About these ads

33 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. 27 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!

    • 28 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?

      • 29 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


  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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




My Recent Tweets

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
« Jul   Sep »
1234567
891011121314
15161718192021
22232425262728
293031  

Follow

Get every new post delivered to your Inbox.

Join 2,328 other followers

%d bloggers like this: