Creating an executable .exe file from a PowerShell Script?

LaPhi picture LaPhi · Sep 21, 2010 · Viewed 77.2k times · Source

Is it possible to create an executable file.exe file from a PowerShell Script?

Answer

Jaykul picture Jaykul · Jan 7, 2011

No. At least, not yet, and after 5 versions of PowerShell, it seems unlikely ever.

I wish I could just leave it at that, but other people have provided a bunch of "workaround" type answers I feel compelled to address:

You can wrap your .ps1 script in another script type to make it double-clickable, and you can even generate an executable with the script embedded (see the other answers on this thread) ... but those "executables" require the right version of PowerShell to be already present on the system, so you're not gaining anything by doing that, and you loose a lot of the features of PowerShell (like streaming object output, help documentation, and automatic parameter handling with tab completion for users).

Here is a simple .cmd batch file wrapper (you could extend this to allow parameters):

REM <#
copy %0 %0.ps1
PowerShell.exe -ExecutionPolicy Unrestricted -NoProfile -Command "&{Set-Alias REM Write-Host; .\%0.ps1}"
del %0.ps1
exit
REM #>

### Your PowerShell script goes below here.
### I've put a couple of lines as an example ...
ls | sort length -desc | select -first 5 | ft
ps | sort ws -desc | select -first 10 | ft

I know ...

With Portable PowerShell, it would probably be possible to package up a sort of self-extracting zip that would contain the right version of PowerShell and a script and would work. That's not an executable in any normal sense of the word -- it's a bit like if Valve had decided to just ship a vmware image on a thumbdrive as their solution to letting Linux users play Half Life. However, the product seems abandoned.

With PrimalScript (or PowerShell Studio) or PowerGui or pShellExec, your script can be encrypted, so it's slightly secured against prying eyes ... but this is basically just obfuscation, and essentially no different than the batch file, and in some ways worse.