PS1ToEXE.psm1

$User_Profile = $env:userprofile
$Global:User_Desktop = "$User_Profile\Desktop"
$Program_Files_x86 = "C:\Program Files (x86)"
$IsX64 = Test-path $Program_Files_x86
$Global:Temp_Folder = $env:TEMP
$Global:Temp_Conf_file = "$Temp_Folder\PS1ToEXE_Generator_Temp.conf"

<#.Synopsis
    The EXE function allows you to create an EXE file from a PowerShell project
    After running the cmdlet, all PS1 files located in your PowerShell project will be listed.
    You'll have to choose the main PS1 file. This one that will be launched after running the EXE file
 
.DESCRIPTION
    -Name: Mandatory - Allows you to type the name of your EXE
    -Path: Mandatory - Allows you to type the path of your PowerShell project
    -Icon: Optional - Allows you to add an icon to your EXE file
    -Password: Allows you to add a password to your EXE. After running the EXE, the password will be prompted
    -Admin: Allows you to require admin rights to run the EXE
     
    EXE -Name "My Application" -Path "F:\MyAppli"
 
.EXAMPLE
    PS Root\> EXE -Name "My Application" -Path "F:\MyAppli"
    The command above will create an EXE called "My Application.exe" and will contain content of the folder "F:\MyAppli"
 
.EXAMPLE
    PS Root\> EXE -Name "My Application" -Path "F:\MyAppli" -admin -Password "Password"
    The command above will create an EXE called "My Application.exe" and will contain content of the folder "F:\MyAppli"
    Admin rights will be required to run the EXE file
    It will add the password "Password" to the EXE
 
.NOTES
    Author: Damien VAN ROBAEYS - @syst_and_deploy - http://www.systanddeploy.com
#>
    
    
Function EXE
{
[CmdletBinding()]
Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true, position=1)]
        [string] $Name,        
        [Parameter(Mandatory=$true,ValueFromPipeline=$true, position=1)]
        [string] $Path,                            
        [Parameter(Mandatory=$False,Position=1)]
        [string] $Icon,
        [Parameter(Mandatory=$False,ValueFromPipeline=$true, position=1)]
        [string] $Password,            
        [Parameter(Mandatory=$False,Position=1)]
        [switch] $Admin        
      )
    Begin
    {            
        Try
            {    
            
                write-host ""                     
                write-host "***********************************************************************" -foregroundcolor "White"    
                write-host " PS1 To EXE Generator Module - v1.0 " -foregroundcolor "Green"                        
                write-host "***********************************************************************" -foregroundcolor "White"                
                
                write-host ""                     
                write-host "***********************************************************************"     
                write-host " WinRAR Checking Part " -foregroundcolor "Green"        
                write-host "***********************************************************************"                     

                If ($IsX64 -eq $true)
                    {                    
                        # Computer is running on x64 archi
                        write-host ""                                                             
                        write-host "Computer is running on x64 architecture" -foregroundcolor "Cyan"        
                                                
                        # Now we'll check the WinRAR version
                        # Winrar x64
                        $Global:Winrar_EXE_x64 = "C:\Program Files\WinRAR\WinRAR.exe"

                        # WinRAR x86 on x64 computer
                        $Global:Winrar_EXE_x86 = "C:\Program Files (x86)\WinRAR\WinRAR.exe"    
                        # Check if an x64 EXE is found
                        $Global:Check_If_Winrar_x64 = Test-Path $Winrar_EXE_x64
                        # Check if an x86 EXE is found
                        $Global:Check_If_Winrar_x86 = Test-Path $Winrar_EXE_x86        
                        
                        If (($Check_If_Winrar_x64 -eq $false) -and ($Check_If_Winrar_x86 -eq $false))
                        # If no x64 or x86 EXE have been found
                            {                            
                                write-host "WinRAR is required!!! It seems that it is not installed on your computer." -foregroundcolor "Yellow"    
                                $Global:Winrar_Status = $false        
                                
                            }    

                        If (($Check_If_Winrar_x64 -eq $true) -and ($Check_If_Winrar_x86 -eq $true))
                        # If both x64 and x86 EXE have been found
                            {            
                                write-host "Both versions x86/x64 of WinRAR are installed" -foregroundcolor "Cyan"    
                                write-host "WinRAR x64 is selected by default" -foregroundcolor "Cyan"                                    
                                $Global:Winrar_Folder = "C:\Program Files (x86)\WinRAR"    
                                $Global:Winrar_Status = $true                                        
                            }            

                        ElseIf (($Check_If_Winrar_x64 -eq $true) -or ($Check_If_Winrar_x86 -eq $true))
                            {        
                                If ($Check_If_Winrar_x64 -eq $true) 
                                    {
                                        write-host "WinRAR x64 is installed" -foregroundcolor "Cyan"    
                                        $Global:Winrar_Folder = "C:\Program Files\WinRAR"    
                                        $Global:Winrar_Status = $true                                                
                                    }
                                ElseIf ($Check_If_Winrar_x86 -eq $true) 
                                    {                                        
                                        write-host "WinRAR x86 is installed" -foregroundcolor "Cyan"                                            
                                        $Global:Winrar_Folder = "C:\Program Files (x86)\WinRAR"        
                                        $Global:Winrar_Status = $true                                                                                        
                                    }            
                            }                
                    }
                Else
                    {
                        # Computer is running on x86 archi
                        write-host ""                                                             
                        write-host "Computer is running on x86 architecture" -foregroundcolor "Cyan"                                
                        $Global:Winrar_EXE_x86 = "C:\Program Files\WinRAR\WinRAR.exe"    
                        
                        # Check if an x86 EXE is found
                        $Global:Check_If_Winrar_x86 = Test-Path $Winrar_EXE_x86        

                        If ($Check_If_Winrar_x86 -eq $false)
                        # If no x86 EXE has been found
                            {        
                                write-host "WinRAR is required!!! It seems that it is not installed on your computer." -foregroundcolor "Yellow"    
                                $Global:Winrar_Status = $false                                                
                            }
                        Else
                            {
                                write-host "WinRAR x86 is installed" -foregroundcolor "Green"                                                                            
                                $Global:Winrar_Folder = "C:\Program Files\WinRAR"    
                                $Global:Winrar_Status = $true                                                                                
                            }        
                    }            
            }

        Catch 
            {        
            }        
    }
    
    Process
    {    
        If ($Winrar_Status -eq $true)
            {
                $EXE_folder = $Path        
                $EXE_PS1_To_Run = $PS1        
                $EXE_File_Name = $Name
                $EXE_Icon_To_Set = $Icon

                $Dir_EXE_Folder = get-childitem $EXE_folder -recurse
                $List_All_PS1 = $Dir_EXE_Folder | where {$_.extension -eq ".ps1"}                
            
                
                write-host ""                                     
                write-host "***********************************************************************"     
                write-host " PS1 Selection Part" -foregroundcolor "Green"        
                write-host "***********************************************************************" 
                write-host ""                                     
                
                $PS1_Choice = @{}
                for ($i=1;$i -le $List_All_PS1.count; $i++) {
                    Write-Host "$i. $($List_All_PS1[$i-1].name)" -foregroundcolor "Cyan"                                            
                    $PS1_Choice.Add($i,($List_All_PS1[$i-1].name))
                    }                    

                [int]$MyPS1 = Read-Host "Please select which PS1 file will be launched after running the EXE"    
                write-host ""                                         
                                    
                $EXE_PS1_To_Run = $PS1_Choice.Item($MyPS1)                    
                New-item $Temp_Conf_file -type file | out-null
                Add-Content $Temp_Conf_file "Path=%temp%", "Overwrite=", "Silent=1", "SavePath"
                Add-Content $Temp_Conf_file "Setup=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -sta -WindowStyle Hidden -noprofile -executionpolicy bypass -file %temp%\$EXE_PS1_To_Run $Parameters"        
                
                If (-not $Password)
                    {        
                        $Global:Password_Status = ""                        
                    }    
                Else
                    {
                        $Global:Password_Status = '"' + '-p' + $Password + '"'            
                    }            
                    
                If (-not $Admin)
                    {
                        $command = "$Winrar_Folder\WinRAR.exe"        
                        & $command a -ep1 -r -o+ -dh -ibck -sfx $Password_Status "-iicon$EXE_Icon_To_Set" "-z$Temp_Conf_file"  "$User_Desktop\$EXE_File_Name.exe" "$EXE_folder\*"        
                    }    
                Else
                    {
                        $command = "$Winrar_Folder\WinRAR.exe"                
                        & $command a -ep1 -r -o+ -dh -ibck -sfx $Password_Status -iadm "-iicon$EXE_Icon_To_Set" "-z$Temp_Conf_file"  "$User_Desktop\$EXE_File_Name.exe" "$EXE_folder\*"    
                    }
            }                    
    }

    end
    {
        sleep 1
        Remove-item $Temp_Conf_file -force | out-null
    }                                
}