Compile PS1 to EXE

This topic contains 14 replies, has 7 voices, and was last updated by Profile photo of Johann Johann 4 months, 1 week ago.

  • Author
    Posts
  • #11838
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    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
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

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

    It's free now! thanks DELL!

  • #15516
    Profile photo of Jeremy Stoltenberg
    Jeremy Stoltenberg
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    }
    
  • #30118
    Profile photo of sunil jadhav
    sunil jadhav
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    [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
    Profile photo of sunil jadhav
    sunil jadhav
    Participant

    Fixed.
    Thanks a lot Dave !

  • #30175
    Profile photo of H Man
    H Man
    Participant
  • #64407
    Profile photo of Johann
    Johann
    Participant

    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

You must be logged in to reply to this topic.