Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an executable .exe file from a PowerShell Script?

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

like image 213
LaPhi Avatar asked Sep 21 '10 10:09

LaPhi


People also ask

Can we create exe from PowerShell script?

To convert a single PowerShell script to EXE via the command-line requires a single line providing the main PS2EXE command ( Invoke-PS2EXE ) followed by the script's path to convert and the path to the EXE you'd like to create. You can now run target.exe, and it will invoke the code defined in the source.

How do I download an exe from PowerShell?

We are going to start with the most common way to download a file from an URL with PowerShell. For this, we will be using the Invoke-WebRequest cmdlet. To download a file we need to know the source URL and give up a destination for the file that we want to download. The parameter -OutFile is required.

How do I make a Windows script executable?

On the Actions menu, click Configure Run Profiles. In the Configure Run Profiles for Management Agent dialog box, in Management agent run profiles, click the run profile that you want to compile into an executable file, and then click Script. In the Save as dialog box, type a name and location for the script.


1 Answers

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.

like image 173
Jaykul Avatar answered Oct 10 '22 05:10

Jaykul