Compile PS1 to EXE

Welcome Forums General PowerShell Q&A Compile PS1 to EXE

Viewing 13 reply threads
  • Author
    Posts
    • #11838
      Participant
      Topics: 11
      Replies: 145
      Points: 48
      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
      Topics: 13
      Replies: 4872
      Points: 1,844
      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
      Topics: 9
      Replies: 2322
      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
      Topics: 11
      Replies: 145
      Points: 48
      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
      Topics: 9
      Replies: 2322
      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
      Topics: 11
      Replies: 145
      Points: 48
      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
      Topics: 11
      Replies: 145
      Points: 48
      Rank: Member

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

      It's free now! thanks DELL!

    • #15516
      Participant
      Topics: 0
      Replies: 2
      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
      Topics: 9
      Replies: 2322
      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
        Topics: 0
        Replies: 2
        Points: 2
        Rank: Member

        That worked perfectly. Thank you Dave!

    • #30118
      Participant
      Topics: 0
      Replies: 2
      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
      Topics: 9
      Replies: 2322
      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
      Topics: 0
      Replies: 2
      Points: 0
      Rank: Member

      Fixed.
      Thanks a lot Dave !

    • #30175
      Participant
      Topics: 29
      Replies: 75
      Points: 0
      Rank: Member
    • #64407
      Participant
      Topics: 0
      Replies: 1
      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

Viewing 13 reply threads
  • The topic ‘Compile PS1 to EXE’ is closed to new replies.