Compile Ps1 To Exe



Reply To: Compile PS1 to EXE 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. For GUI, execute in powershell.exe: win-ps2exe. Fill in the fields: Source file:. Browse for your source “.ps1” file. Target file:. Fill the full path to the exported executable. Icon file:. This one isn’t mandatory, but it would distinguish your EXE from others. 4.2.

Hello everybody, new place new posts…

Sometimes I need to convert my PowerShell scripts (.ps1) into Executable files (.exe) for many different reasons. I’m sure you’ll face or you’ve already faced one similar situation too .

Once a client’s 4th line team has been asked by MS support to collect different logs from 6K+ workstations and send it back to them. The tricky part was the log collection should be started at the time of having the issue. Ok for 1 to 5 workstations you can ask your service desk personnel to get contact, explain the end-user that they’ve to contact service desk when they’ve the issue and at that time service desk personnel should launch that and that scripts in that special order. Finally when it finishes collect the different logs from different paths and zip them and then send it back to 4th line for sending back it to MS. But what if Service desk guy forgets to run the scripts in order ?? what if the end-user can’t contact the service desk guy at the time of issue because suddenly his skype or his workstation doesn’t respond ??

Compile ps1 to exe installer

Wow wow woow.. what a long story … lots of things should be done in order.. but hey wait what if we write a PowerShell script which runs all required commands in order, collects the logs somewhere on the workstation and zips the logs , then send logs to a central share somewhere on the network ??? that would be cool isn’t it ?? you’d just check that central share sometimes and send the collected zipped log files to MS . You eliminate lots of human errors.. That would be even better if you could deploy your PowerShell script on end-user’ desktops for example and ask them to click on it once when they have the issue.. but in Enterprise environments a standard user would probably have some issues when launching a PowerShell script and also it would be difficult to explain how to launch a PowerShell script. Some of them may even try to modify your script. The best solution can be just to convert your PowerShell script into an executable (.exe) file , deploy it on end-users’ desktops by using GPP or ConfigMgr for example and ask the user to click on this exe when the issue happens. Easy solution just needs end-user’s simple click then everything is done automatically 🙂

I know I know .. now there are lots of alternatives, but what I use since a long time is PS2EXE which can be found here (author is Ingo Karstein ): https://gallery.technet.microsoft.com/scriptcenter/PS2EXE-Convert-PowerShell-9e4e07f1

I’ve also seen the following versions (but never tried, you can try them at your own risk) :
– Azure version: https://ps2exe.azurewebsites.net/
– GUI version: https://gallery.technet.microsoft.com/scriptcenter/PS2EXE-GUI-Convert-e7cb69d5

Before to start to explain PS2EXE you should know that PS2EXE itself is a PowerShell script that converts your PowerShell script to Executable. In fact your the tool encapsulates your PowerShell script to an executable file.

More description about it can be found in the related PS2EXE TechNet link : “It does not convert the PowerShell script to an other language! It encapsulates the script with a lightweight PowerShell host written in C# and compiles the dynamically generated C# source code in memory to an EXE file. The resulting EXE is an .NET assembly that contains the source script encoded in Base64. The EXE includes all stuff that is needed to execute an PowerShell through the .NET object model. It is based on classes in the namespace System.Management.Automation that reperents the PowerShell engine. – Therefore the EXE file is not a real “standalone” EXE file. It needs PowerShell to be installed!!! And – of course – it needs .NET Framework v2.0. Furthermore “script execution” have to be allowed (see cmdlet: set-execultionpolicy). – The resulting EXE is “MSIL” and is able to execute as x64 or x86.”

Compile Powershell

Ok but how it works ??

Me, I’ve a folder on my PC with PS2EXE in it:

Compile Ps1 To Exe Installer

When I need to convert a PowerShell script to Executable I just move the .ps1 file here :

Then I launch my PowerShell and cd to this directory :

Usage is fairly easy: ./PS2EXE.ps1 –InputFile “PATHName_of_script_to_be_ converted.ps1” –OutputFile “PATHName_of_excutable_file.exe”

But before to run it check your execution policy:

You can find detailed explanation about Execution Policies here.

If it’s Restricted then you can to use the following line to bypass execution policy during your script execution: powershell –ExecutionPolicy Bypass

Compile Ps1 To Exe

That will start a PowerShell session that allows for running scripts and keeps the lowered permissions isolated to just the current running process.

Now just run the PS2EXE :

And your .exe file is ready to use !!

Easy isn’t it ?? Hope that helps you one day !! what about the script ? I’ll post it next time 😉

/sencer/

( This information is provided “AS IS” with no warranties , use it at your own risk )