Compile PS1 to EXE

Welcome Forums General PowerShell Q&A Compile PS1 to EXE

This topic contains 14 replies, has 7 voices, and was last updated by

 
Participant
1 year, 10 months ago.

  • Author
    Posts
  • #11838

    Participant
    Points: 1
    Rank: Member

    What is the best "free" way to compile PS1 files to EXE. I have read that there's something in the community extensions and also in PowerGUI Pro but I do not want to pay for that if I don't have to. I downloaded the PSCX but it's an MSI package and I'm having trouble getting it to install.

    I can delete and re download that but has anyone used it? Are there better "free" recommendations for compiling to EXE?

    -VERN

  • #11839

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    You're not technically compiling, you know that, right? It's "packaging." It wraps an EXE around the script, and executes in-memory. Just to be sure.

    PowerGUI is free; there's no longer a pay version, I don't think.

  • #11840

    Member
    Points: 0
    Rank: Member

    What Don said. Anything that claims to be "compiling" a script is really just creating a self-extracting zip file, or something very similar.

    I've done that with PowerGUI at client's request, a couple of times. It makes some things really annoying; for example, if you have code to check the script's current directory ($PSScriptRoot , $MyInvocation.MyCommand.Path , etc), it'll start referring to a temporary folder instead of the folder where the .exe file was located.

  • #11860

    Participant
    Points: 1
    Rank: Member

    I have read that and I am OK with that fact. Just being an EXE might help me work around another issue I'm having. I don't mind that it's just wrapped up into an EXE.

    I am forced to use an API to run my scripts through a non Microsoft RBA and orchestration system.

  • #11861

    Member
    Points: 0
    Rank: Member

    PowerGUI should do the trick, then. One thing to keep in mind is that if you need to pass parameters to your script and it's compiled to an EXE by PowerGUI, you need to stick an extra parameter in there (-Arguments, if I remember right), ie:

    SomeCompiledScript.exe -Arguments -ScriptParam1 Value1 -ScriptParam2 Value2

  • #11862

    Participant
    Points: 1
    Rank: Member

    Thanks guys anyone have a download link to the Pro version?

    I looked around http://powergui.org/index.jspa and I'm having trouble finding the Pro version Don was talking about.

    I downloaded the version here http://powergui.org/downloads.jspa but the compile menu is grayed out.

  • #11863

    Participant
    Points: 1
    Rank: Member

    duh! helps when you have an open script in the editor window. Never mind!

    It's free now! thanks DELL!

  • #15516

    Participant
    Points: 2
    Rank: Member

    What Don said. Anything that claims to be “compiling” a script is really just creating a self-extracting zip file, or something very similar.

    I've done that with PowerGUI at client's request, a couple of times. It makes some things really annoying; for example, if you have code to check the script's current directory ($PSScriptRoot , $MyInvocation.MyCommand.Path , etc), it'll start referring to a temporary folder instead of the folder where the .exe file was located.

    Did you ever find a way around this problem? I'm having the same issue and don't like the idea of hard coding a path in.

  • #15526

    Member
    Points: 0
    Rank: Member

    It's been a while, but I think I wound up doing something like this:

    $scriptRoot = [System.AppDomain]::CurrentDomain.BaseDirectory.TrimEnd('\')
    if ($scriptRoot -eq $PSHOME.TrimEnd('\'))
    {
        $scriptRoot = $PSScriptRoot
    }
    
    • #15587

      Participant
      Points: 2
      Rank: Member

      That worked perfectly. Thank you Dave!

  • #30118

    Participant
    Points: 0
    Rank: Member

    Hi Jeremy, Dave

    I have the same issue, .exe is referring to a temporary folder. So you could you please explain the work around. I didn't understand – I have been struggling with this from a number of days. I am so glad I found this blog.

  • #30120

    Member
    Points: 0
    Rank: Member

    [System.AppDomain]::CurrentDomain.BaseDirectory will refer to the location of the executable that is running. Normally that's the $pshome folder (where PowerShell.exe and PowerShell_ISE.exe are located), but when you are using a compiled script generated by PowerGUI, [System.AppDomain]::CurrentDomain.BaseDirectory refers to the folder where the exe is found instead.

  • #30121

    Participant
    Points: 0
    Rank: Member

    Fixed.
    Thanks a lot Dave !

  • #30175

    Participant
    Points: 0
    Rank: Member
  • #64407

    Participant
    Points: 0
    Rank: Member

    Hi,

    I had the same issue. I tried the solution of Dave but it brought the path of Powershell.
    So I made that :

    $CurrentProcess = [System.Diagnostics.Process]::GetCurrentProcess() | Select-Object -ExpandProperty Id
    $ParentProcess = gwmi Win32_Process -Filter "processid = '$CurrentProcess'" | Select-Object -ExpandProperty ParentProcessId
    $ProcessPath = Get-Process -Id $ParentProcess  | Select-Object -ExpandProperty Path | Split-Path
    

    It gets the parent process of the Powershell executed by the script, this parent process is the EXE file.
    The $ProcessPath is the path of your EXE file.

    Johann

The topic ‘Compile PS1 to EXE’ is closed to new replies.