In this article, I will demonstrate how to build an EXE to run your PowerShell scripts with WinRAR. You will learn how to create an EXE using the WinRAR GUI, and automate this task using PowerShell.

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
  1. You start by opening WinRAR and click on the Wizard button.

    Click the Wizard button

    Click the Wizard button

  2. Select the file to run using the EXE > MyScript.ps1
  3. Check the option Create a new archive

    Create a new archive button

    Create a new archive button

  4. Type your EXE name > My first EXE

    Type your EXE name

    Type your EXE name

  5. Check the option “Create a self-extracting (.exe) archive

    Create self-extracting archive

    Create self-extracting archive

  6. The EXE will be created on your desktop. It isn’t yet ready to run our ps1 file, it’ll just extract it.
  7. To that end open the My_first_EXE.exe file with WinRAR and click the SFX button.
  8. Then select the Comment tab and copy the text below in the comment field
    Comment tab

    Comment tab

    Comment text

     Setup=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -sta -WindowStyle Hidden -noprofile -executionpolicy bypass -file %temp%\MyScript.ps1
  9. Let’s add an icon to our EXE file. This can be achieved by clicking on the Advanced SFX options.

    Advanced SFX option

    Advanced SFX option

  10. Then click the Text and Icon tab and the Browse button for Load SFX icon from this file.
    Text and icon tab

    Text and icon tab

    Load SFX icon from the file

    Load SFX icon from the file

  11. Once it’s done, the EXE below is created on your desktop.

    EXE file

    EXE file

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.

Your EXE information

Your EXE information

As you can see in the following screenshot, after entering the EXE information, the process will start.

EXE has been created

EXE has been created

 $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 /?

  1. JimmyDerp 7 years ago

    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!”

  2. wand3rvogel 7 years ago

    Discover free 7z SFX module by Oleg N. Scherbakov:

  3. Rory 6 years ago

    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.

  4. aaa 4 years ago

    Just correcting a minor mistake

    r = Repair archive

    -r = Recurse subdirectories

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account