### Generate Examples with Cmd Source: https://github.com/mscholtes/ps2exe/blob/master/Examples/Readme.md Execute this batch script in Cmd to generate examples. Examples with '-GUI' are compiled with -NoConsole flag. ```cmd .\BuildExamples.bat ``` -------------------------------- ### Generate Examples with PowerShell Source: https://github.com/mscholtes/ps2exe/blob/master/Examples/Readme.md Execute this script in PowerShell to generate examples. Examples with '-GUI' are compiled with -NoConsole flag. ```powershell .\BuildExamples.ps1 ``` -------------------------------- ### Install and Verify PS2EXE Source: https://context7.com/mscholtes/ps2exe/llms.txt Commands to install the module from the PowerShell Gallery and verify the installation. ```powershell # Install PS2EXE from PowerShell Gallery Install-Module ps2exe # Import the module (optional, auto-imports on first use) Import-Module ps2exe # Verify installation Get-Module ps2exe -ListAvailable ``` -------------------------------- ### Launch graphical interface Source: https://context7.com/mscholtes/ps2exe/llms.txt Start the Win-PS2EXE GUI for script compilation. ```powershell # Launch the graphical interface Win-PS2EXE # Alternative launch methods Win-PS2EXE.exe ``` -------------------------------- ### Install PS2EXE Module Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Installs the ps2exe module from the PowerShell Gallery. Ensure you have PowerShell 5.x or later. ```powershell PS C:\> Install-Module ps2exe ``` -------------------------------- ### Launch Win-PS2EXE GUI Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Starts the graphical front end for PS2EXE, providing a user-friendly interface for compiling scripts. ```powershell Win-PS2EXE ``` -------------------------------- ### Retrieve Script/Executable Path Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Use this code to reliably get the path of the script or executable, regardless of whether it's compiled or not. It handles different invocation types. ```powershell if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") { $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition } else { $ScriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) if (!$ScriptPath){ $ScriptPath = "." } } ``` -------------------------------- ### Advanced Compilation with Full Options Source: https://context7.com/mscholtes/ps2exe/llms.txt Configure metadata, administrative requirements, and embedded resources for a production-ready GUI application. ```powershell # Full-featured GUI application compilation Invoke-ps2exe -inputFile ".\MyScript.ps1" ` -outputFile ".\Release\MyApplication.exe" ` -noConsole ` -iconFile ".\Resources\app.ico" ` -title "My Application" ` -description "A powerful automation tool" ` -company "My Company Inc." ` -product "My Application Suite" ` -copyright "Copyright 2024 My Company Inc." ` -trademark "MyApp is a trademark" ` -version "2.1.0.0" ` -requireAdmin ` -DPIAware ` -supportOS ` -STA ` -configFile ` -exitOnCancel ` -embedFiles @{ '.\config.json'='.\config.json' '.\Resources\template.html'='.\templates\report.html' } ` -verbose Write-Host "Application compiled successfully!" ``` -------------------------------- ### Enable credential GUI Source: https://context7.com/mscholtes/ps2exe/llms.txt Provide graphical prompts for credentials in console applications. ```powershell # Console app with GUI credential prompts ps2exe .\AuthScript.ps1 .\AuthApp.exe -credentialGUI # Combined with other options ps2exe .\AuthScript.ps1 .\AuthApp.exe -credentialGUI -requireAdmin ``` -------------------------------- ### Compile for Specific Architectures Source: https://context7.com/mscholtes/ps2exe/llms.txt Commands to target 32-bit or 64-bit architectures, overriding the default 'Any CPU' behavior. ```powershell # Compile for 32-bit systems only ps2exe .\MyScript.ps1 .\MyScript-x86.exe -x86 # Compile for 64-bit systems only ps2exe .\MyScript.ps1 .\MyScript-x64.exe -x64 # 32-bit GUI application ps2exe .\MyScript.ps1 .\MyScript-x86-GUI.exe -x86 -noConsole # Application virtualization (forces x86) ps2exe .\MyScript.ps1 .\VirtualApp.exe -virtualize ``` -------------------------------- ### Configure runtime settings Source: https://context7.com/mscholtes/ps2exe/llms.txt Generate configuration files or enable specific runtime features like long paths or DPI awareness. ```powershell # Generate config file ps2exe .\MyScript.ps1 .\MyApp.exe -configFile # Enable long paths support (requires config file) ps2exe .\MyScript.ps1 .\MyApp.exe -longPaths # WinForms DPI awareness (auto-enables config file) ps2exe .\MyScript.ps1 .\MyApp.exe -noConsole -winFormsDPIAware ``` -------------------------------- ### Compile GUI Applications Source: https://context7.com/mscholtes/ps2exe/llms.txt Commands to create Windows Forms applications without a console window, including options for metadata and DPI awareness. ```powershell # Create GUI application without console window ps2exe .\MyScript.ps1 .\MyScript-GUI.exe -noConsole # GUI app with custom icon and metadata ps2exe .\MyScript.ps1 .\MyApp.exe ` -noConsole ` -iconFile .\app.ico ` -title "My Application" ` -version "1.0.0.0" ` -company "My Company" ` -product "My Product" ` -copyright "Copyright 2024" # GUI app with DPI awareness for high-resolution displays ps2exe .\MyScript.ps1 .\MyApp.exe -noConsole -DPIAware # Exit application when user cancels input dialogs ps2exe .\MyScript.ps1 .\MyApp.exe -noConsole -exitOnCancel ``` -------------------------------- ### Compile Console Apps with Elevated Privileges Source: https://context7.com/mscholtes/ps2exe/llms.txt Commands to create executables that trigger UAC elevation upon launch. ```powershell # Create console app requiring administrator rights ps2exe .\AdminScript.ps1 .\AdminTool.exe -requireAdmin # Admin GUI tool with custom branding ps2exe .\AdminScript.ps1 .\AdminTool.exe ` -requireAdmin ` -noConsole ` -iconFile .\admin.ico ` -title "Admin Tool" ` -description "Administrative utility requiring elevated privileges" # Admin tool with verbose output during compilation ps2exe .\AdminScript.ps1 .\AdminTool.exe -requireAdmin -verbose ``` -------------------------------- ### PS2EXE Command Syntax Source: https://github.com/mscholtes/ps2exe/blob/master/README.md The primary command structure for converting a PowerShell script into an executable file. ```powershell ps2exe [-inputFile] '' [[-outputFile] ''] [-prepareDebug] [-x86|-x64] [-lcid ] [-STA|-MTA] [-noConsole] [-conHost] [-UNICODEEncoding] [-credentialGUI] [-iconFile ''] [-$embedFiles ] [-title ''] [-description '<description>'] [-company '<company>'] [-product '<product>'] [-copyright '<copyright>'] [-trademark '<trademark>'] [-version '<version>'] [-configFile] [-noOutput] [-noError] [-noVisualStyles] [-exitOnCancel] [-DPIAware] [-requireAdmin] [-supportOS] [-virtualize] [-longPaths] ``` -------------------------------- ### Force legacy console host Source: https://context7.com/mscholtes/ps2exe/llms.txt Use -conHost to force the use of the classic console host instead of Windows Terminal. ```powershell # Force classic ConHost console ps2exe .\MyScript.ps1 .\MyApp.exe -conHost # ConHost with specific encoding ps2exe .\MyScript.ps1 .\MyApp.exe -conHost -UNICODEEncoding ``` -------------------------------- ### Prevent Background Window Activation Source: https://github.com/mscholtes/ps2exe/blob/master/README.md This workaround uses $Host.UI.RawUI.FlushInputBuffer() to open and close an invisible window, preventing subsequent windows from opening in the background when running in -noConsole mode. ```powershell $Host.UI.RawUI.FlushInputBuffer() ipconfig | Out-String $Host.UI.RawUI.FlushInputBuffer() ``` -------------------------------- ### Compile Scripts with Invoke-ps2exe Source: https://context7.com/mscholtes/ps2exe/llms.txt Basic compilation commands to convert PowerShell scripts into standalone executables. ```powershell # Basic compilation - creates MyScript.exe in the same directory Invoke-ps2exe .\MyScript.ps1 # Alternative using the alias ps2exe .\MyScript.ps1 # Specify output file name Invoke-ps2exe .\MyScript.ps1 .\Output\MyApplication.exe # Compile to a specific folder (uses original filename) ps2exe .\MyScript.ps1 .\Output\ ``` -------------------------------- ### Generated Executable Reserved Parameters Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Arguments available to the resulting executable at runtime to control execution, debugging, or extraction. ```text -? [<MODIFIER>] Powershell help text of the script inside the executable. The optional parameter combination "-? -detailed", "-? -examples" or "-? -full" can be used to get the appropriate help text. -debug Forces the executable to be debugged. It calls "System.Diagnostics.Debugger.Launch()". -extract:<FILENAME> Extracts the powerShell script inside the executable and saves it as FILENAME. The script will not be executed. -wait At the end of the script execution it writes "Hit any key to exit..." and waits for a key to be pressed. -end All following options will be passed to the script inside the executable. All preceding options are used by the executable itself and will not be passed to the script. ``` -------------------------------- ### CLI Command: ps2exe Source: https://github.com/mscholtes/ps2exe/blob/master/README.md The primary command for converting a PowerShell script into an executable file with various configuration options. ```APIDOC ## ps2exe ### Description Converts a PowerShell script into an executable file. The input script must be UTF8 or UTF16 encoded. ### Parameters #### Positional Parameters - **inputFile** (string) - Required - The PowerShell script to convert. - **outputFile** (string) - Optional - The destination executable file name or folder. #### Optional Flags and Parameters - **prepareDebug** (switch) - Create helpful information for debugging. - **x86 / x64** (switch) - Compile for 32-bit or 64-bit runtime. - **lcid** (int) - Location ID for the compiled executable. - **STA / MTA** (switch) - Set 'Single Thread Apartment' or 'Multi Thread Apartment' mode. - **noConsole** (switch) - Resulting executable is a Windows Forms app without a console window. - **conHost** (switch) - Force start with conhost instead of Windows Terminal. - **UNICODEEncoding** (switch) - Encode output as UNICODE in console mode. - **credentialGUI** (switch) - Use GUI for prompting credentials in console mode. - **iconFile** (string) - Icon file name for the compiled executable. - **embedFiles** (hashtable) - Files to embed (e.g., @{'Target'='Source'}). - **title / description / company / product / copyright / trademark / version** (string) - Metadata for the executable properties. - **configFile** (switch) - Write a config file (.exe.config). - **noOutput / noError** (switch) - Suppress standard or error output. - **noVisualStyles** (switch) - Disable visual styles (only with -noConsole). - **exitOnCancel** (switch) - Exit program when Cancel/X is selected (only with -noConsole). - **DPIAware** (switch) - Enable GUI scaling (only with -noConsole). - **requireAdmin** (switch) - Run only in elevated context. - **supportOS** (switch) - Use functions of newest Windows versions. - **virtualize** (switch) - Application virtualization activated. - **longPaths** (switch) - Enable long paths (> 260 characters). ``` -------------------------------- ### Batch Compile PowerShell Scripts Source: https://context7.com/mscholtes/ps2exe/llms.txt Automate the creation of both console and GUI executable versions for all scripts within a specific directory. ```powershell # Compile all .ps1 files in a directory to both console and GUI versions $ScriptPath = Split-Path $MyInvocation.MyCommand.Path -Parent Get-ChildItem "$ScriptPath\*.ps1" | ForEach-Object { # Create console version Invoke-ps2exe $_.FullName ($_.FullName -replace '\.ps1$', '.exe') -verbose # Create GUI version Invoke-ps2exe $_.FullName ($_.FullName -replace '\.ps1$', '-GUI.exe') -verbose -noConsole } Write-Host "Compilation complete" ``` -------------------------------- ### Runtime parameters for compiled executables Source: https://context7.com/mscholtes/ps2exe/llms.txt Use these built-in parameters at runtime for debugging, help, or script extraction. ```powershell # Show help for the compiled script .\MyApp.exe -? # Show detailed help .\MyApp.exe -? -detailed # Show examples from script help .\MyApp.exe -? -examples # Show full help documentation .\MyApp.exe -? -full # Extract the PowerShell script from executable .\MyApp.exe -extract:extracted_script.ps1 # Launch debugger on startup .\MyApp.exe -debug # Wait for keypress before exiting .\MyApp.exe -wait # Pass all following parameters to the embedded script .\MyApp.exe -end -customParam "value" ``` -------------------------------- ### Enable debug mode Source: https://context7.com/mscholtes/ps2exe/llms.txt Generate debugging information and preserve source code for troubleshooting. ```powershell # Compile with debug information ps2exe .\MyScript.ps1 .\MyApp.exe -prepareDebug # Debug version of GUI application ps2exe .\MyScript.ps1 .\MyApp-Debug.exe -noConsole -prepareDebug -verbose ``` -------------------------------- ### Reserved Executable Parameters Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Parameters available within the generated executable itself to control its runtime behavior. ```APIDOC ## Reserved Executable Parameters ### Description These parameters are available for use on the generated .exe file. ### Parameters - **-?** (modifier) - Displays PowerShell help text of the script inside the executable. Supports -detailed, -examples, or -full. - **-debug** (flag) - Forces the executable to be debugged via System.Diagnostics.Debugger.Launch(). - **-extract:<FILENAME>** (string) - Extracts the PowerShell script inside the executable to the specified file without executing it. - **-wait** (flag) - Prompts 'Hit any key to exit...' at the end of execution. - **-end** (flag) - Separator; all following options are passed to the internal script, while preceding options are used by the executable. ``` -------------------------------- ### Control threading apartment state Source: https://context7.com/mscholtes/ps2exe/llms.txt Configure the COM threading model for the executable using -STA or -MTA. ```powershell # Explicitly set Single Thread Apartment (default) ps2exe .\MyScript.ps1 .\MyApp.exe -STA # Use Multi Thread Apartment mode ps2exe .\MyScript.ps1 .\MyApp.exe -MTA # GUI applications typically require STA ps2exe .\GuiScript.ps1 .\MyApp.exe -noConsole -STA ``` -------------------------------- ### Suppress output and errors Source: https://context7.com/mscholtes/ps2exe/llms.txt Use -noOutput and -noError to create silent executables. ```powershell # Suppress all standard output (includes verbose and information) ps2exe .\SilentScript.ps1 .\Silent.exe -noOutput # Suppress all error output (includes warning and debug) ps2exe .\MyScript.ps1 .\NoErrors.exe -noError # Completely silent execution ps2exe .\BackgroundTask.ps1 .\Silent.exe -noOutput -noError # Silent GUI application ps2exe .\MyScript.ps1 .\SilentGUI.exe -noConsole -noOutput -noError ``` -------------------------------- ### Embed Files into Executables Source: https://context7.com/mscholtes/ps2exe/llms.txt Command to embed additional files into the executable using a hashtable mapping. ```powershell # Embed a single configuration file ps2exe .\MyScript.ps1 .\MyApp.exe -embedFiles @{ '.\config.json'='.\config.json' } ``` -------------------------------- ### Prevent Background Windows in GUI Mode Source: https://context7.com/mscholtes/ps2exe/llms.txt Call FlushInputBuffer before and after credential prompts to ensure the window remains in the foreground when using the -noConsole flag. ```powershell # Script for GUI mode with credential prompt # Keeps credential window in foreground $Host.UI.RawUI.FlushInputBuffer() $credential = Get-Credential -Credential "$ENV:USERNAME" $Host.UI.RawUI.FlushInputBuffer() if ($credential) { Write-Host "Authentication successful for: $($credential.UserName)" } else { Write-Host "Authentication cancelled" } ``` -------------------------------- ### Compile PowerShell Script to EXE Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Compiles a PowerShell script into a Windows executable. If the output file is omitted, it defaults to the script's name with a .exe extension. ```powershell Invoke-ps2exe .\source.ps1 .\target.exe ``` ```powershell ps2exe .\source.ps1 .\target.exe ``` -------------------------------- ### Embed files into executable Source: https://context7.com/mscholtes/ps2exe/llms.txt Use the -embedFiles parameter to include external files within the compiled executable at specified target locations. ```powershell ps2exe .\MyScript.ps1 .\MyApp.exe -embedFiles @{ '.\data\settings.xml'='.\settings.xml' '%TEMP%\helper.dll'='.\libs\helper.dll' '.\resources\icon.png'='.\assets\icon.png' } ``` ```powershell ps2exe .\MyScript.ps1 .\MyApp.exe ` -noConsole ` -embedFiles @{ '.\template.html'='.\templates\report.html' '.\style.css'='.\templates\style.css' } ``` -------------------------------- ### Decompile Executable Source: https://github.com/mscholtes/ps2exe/blob/master/README.md Use the -extract parameter to decompile a compiled script. This is a security risk if passwords are stored in the script. ```powershell Output.exe -extract:C:\Output.ps1 ``` -------------------------------- ### Resolve Script Path in Compiled Executables Source: https://context7.com/mscholtes/ps2exe/llms.txt Use this pattern to consistently retrieve the directory path whether the script is running as a raw .ps1 file or a compiled .exe. ```powershell # Get script path that works in both compiled and non-compiled mode if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") { # Running as PowerShell script $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition } else { # Running as PS2EXE compiled executable $ScriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) if (!$ScriptPath) { $ScriptPath = "." } } Write-Host "Directory of executable/script: $ScriptPath" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.