- Add and remove Windows with drivers in MDT with PowerShell - Fri, Jun 24 2016
- Creating an EXE for your PowerShell scripts using WinRAR - Thu, May 19 2016
- Using the Microsoft Deployment Toolkit (MDT) as a portable application - Tue, May 10 2016
Creating an EXE using WinRAR ^
WinRAR is known to compress or decompress files. Although this is the most common use of this tool, it also offers other functionalities. One of these is to create a self-extracting archive (SFX) with an EXE extension. Using this method allows you to choose a file that will be launched after running your EXE file and set an icon to your EXE file.
Let’s see how to build the EXE with the help of the WinRAR GUI.
Consider the following configuration for the step by step guide and script used in this article:
- ps1 > Script to launch when we run the EXE
- ico > Icon for our EXE file
- My first EXE > Name of our EXE file
- %temp% > Path where our EXE will be extracted
- You start by opening WinRAR and click on the Wizard button.
- Select the file to run using the EXE > MyScript.ps1
- Check the option Create a new archive
- Type your EXE name > My first EXE
- Check the option “Create a self-extracting (.exe) archive”
- The EXE will be created on your desktop. It isn’t yet ready to run our ps1 file, it’ll just extract it.
- To that end open the My_first_EXE.exe file with WinRAR and click the SFX button.
- Then select the Comment tab and copy the text below in the comment field
Comment text
Path=%temp% SavePath Setup=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -sta -WindowStyle Hidden -noprofile -executionpolicy bypass -file %temp%\MyScript.ps1 Silent=1 Overwrite=1
- Let’s add an icon to our EXE file. This can be achieved by clicking on the Advanced SFX options.
- Then click the Text and Icon tab and the Browse button for Load SFX icon from this file.
- Once it’s done, the EXE below is created on your desktop.
Automating the EXE creation with PowerShell ^
The next script allows you to automate the tasks mentioned earlier: (a) create an SFX archive, (b) apply the comment part to the EXE file and (c) attribute an icon to our EXE file.
In this script we will use the same configuration as in the script above:
- PS1 script to launch > ps1
- Icon for the EXE > ico
- EXE name > My first EXE
To use the comment part, create a “.conf” file that contains the comment part mentioned above. This file will be stored in the variable $My_Conf_File.
Note: Remove the setup part below from the .conf file. It will be added using the script.
Setup=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -sta -WindowStyle Hidden -noprofile -executionpolicy bypass -file %temp%\MyScript.ps1
The following variables will also be used:
- $Temp_Folder > Path to your temp folder
- $User_Profile > Path to the current user profile
- $Global:User_Desktop > Path to the current user desktop
- $Global:Winrar_Folder > Path to your WinRAR folder
- $Global:Temp_Conf_file > Temp conf file which will be created to add the commandline
This script will proceed as below:
- Copies your .conf file in %temp%
- Adds the command line to run the ps1 file you have typed
- Creates the SFX archive and set the icon file
At your administrative command prompt, type the required information.
As you can see in the following screenshot, after entering the EXE information, the process will start.
Param ( [Parameter(Mandatory=$true)] [String]$EXE_Name, [Parameter(Mandatory=$true)] [String]$Sources_Folder, [Parameter(Mandatory=$true)] [String]$PS1_Name, [Parameter(Mandatory=$true)] [String]$Conf_File, [Parameter(Mandatory=$true)] [AllowEmptyString()] [String]$Icon_File ) $Temp_Folder = $env:TEMP $User_Profile = $env:userprofile $Global:User_Desktop = "$User_Profile\Desktop" $Global:Winrar_Folder = "C:\Program Files\WinRAR" $Global:Temp_Conf_file = "$Temp_Folder\My_Conf_File.conf" copy-item $Conf_File $Temp_Conf_file Add-Content $Temp_Conf_file "Setup=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -sta -WindowStyle Hidden -noprofile -executionpolicy bypass -file %temp%\$PS1_Name" $command = "$Winrar_Folder\WinRAR.exe" & $command a -ep1 -r -o+ -dh -ibck -sfx -iadm "-iicon$Icon_File" "-z$Temp_Conf_file" "$User_Desktop\$EXE_Name.exe" "$Sources_Folder\*" sleep 5 remove-item $Temp_Conf_file -force write-host "" write-host "******************************************************" -foregroundcolor "cyan" write-host "The EXE $EXE_Name has been created on your desktop" -foregroundcolor "yellow" write-host "******************************************************" -foregroundcolor "cyan" Following are the different WinRAR switches used in the script: # a: Add files to archive # -ep1: Exclude base directory from names # -r: Repair an archive # -o+: Overwrite all # -dh: Open shared files # -ibck: Run Winrar in Background # -sfx: Create an SFX self-extracting archive # -iadm: request administrative access for SFX archive # -iiconC: Specify the icon # -z: Read archive comment from file
See more options using rar.exe /?
Can you create a self-extracting Powershell script to create a self-extracting Powershell script?
“yo dawg I heard you like self-extracting Powershell scripts so we created a Powershell script that self-extracts and creates a self-extracting Powershell script that extracts itself and creates a self-extracting Powershell script!”
Discover free 7z SFX module by Oleg N. Scherbakov:
http://www.7zsfx.info/en/examples.html
Nice one! I did something similar with 7-zip a few years ago. Had some apps that third party consultants needed and provided the company VPN software the same way.
Thanks Rory. I never used 7Zip to do this but I’ll try.
Just correcting a minor mistake
r = Repair archive
-r = Recurse subdirectories