Converting PowerShell script into an Executable/Application

Converting PowerShell script into an Executable/Application

It’s very often need to convert a PowerShell script into an Executable/Application to hand over to third party without disclosing the source of your scripts.

 

Here are few simple steps that lets you have this done:

  • Download PS2EXE script from CodePlex project here.
  • Have your script ready which you would like to convert into a standalone Executable/Application
  • Run PS2EXE to have your script converted into an Executable/Application

 

Example:

      Say you create a one line script that launches Notepad as shown below:

    C:Temp>echo Invoke-Item “C:Windowssystem32notepad.exe” >> Invoke-Notepad.ps1

    C:Temp>type Invoke-Notepad.ps1
    Invoke-Item “C:Windowssystem32notepad.exe”

    C:Temp>

    PS C:Temp> .callPS2EXE.bat “.Invoke-Notepad.ps1” “.Invoke-Notepad.exe” -x86 -noconsole
    PS2EXE; v0.3.0.0 by Ingo Karstein (http://blog.karstein-consulting.com)

    You are using PowerShell 3.0.

    winexe
    Reading input file C:TempInvoke-Notepad.ps1

    Compiling file…

    Output file C:TempInvoke-Notepad.exe written
    Config file for EXE created.
    PS C:Temp>

    PS C:Temp> gci Invoke-*

    Directory: C:Temp

    Mode                LastWriteTime     Length Name
    —-                ————-     —— —-
    -a—          6/4/2013   8:25 PM      28672 Invoke-Notepad.exe
    -a—          6/4/2013   8:25 PM        161 Invoke-Notepad.exe.config
    -a—          6/4/2013   8:23 PM         48 Invoke-Notepad.ps1

    PS C:Temp> invoke-item Invoke-Notepad.exe
    PS C:Temp>

     

     

    PS2EXE Usage:

    Call  the script with this parameters:

    -inputFile PowerShell script file
    -outputFile file name (with path) for the destination EXE file
    -debug (switch) generate debug info in the destination EXE file. The dynamically generated .CS file will stored beside the output EXE file. Furthermore a .PDB file will be generated for the EXE file
    -verbose (switch) shows also verbose informations – if any.
    -x86 (switch) compile EXE to run as 32 bit application
    -x64 (switch) compile EXE to run as 64 bit application
    -runtime20 (switch) force running the EXE in PowerShell 2.0 using .NET 2.0
    -runtime30 (switch) force running the EXE in PowerShell 3.0 using .NET 4.0
    -lcid specify language ID for threads
    -sta run PowerShell environment in Single Thread Apartment mode
    -mta run PowerShell environment in Multithread Apartment mode
    -noconsole compile PS script as Windows application

     

     

    References:

    http://ps2exe.codeplex.com/

    3 thoughts on “Converting PowerShell script into an Executable/Application

    Leave a Reply

    Your email address will not be published. Required fields are marked *