Back to Browse

Make Portable PowerShell EXEs Without External Dependencies

1.1K views
Oct 24, 2024
11:11

In this video, Brien Posey answers a viewer’s question about external dependencies when converting PowerShell scripts into executable files via PS2EXE. He explains how to manage dependencies, such as image files, by embedding them directly within the PowerShell script. By the end, you’ll understand how to create a fully portable PowerShell executable without relying on external files. The example PowerShell scripts in this video are displayed below. 0:00 Handling External Dependencies 01:17 An Example PowerShell Script with an External Dependency 03:48 Incorporating the External Dependency into the PS Script 04:03 Convert the JPG File to a Base64 Encoded String & Output the Image 05:27 Add the Base64 Encoded Image String 06:29 Decode the Base64 String to a Byte Array 06:51 Create a MemoryStream from the Byte Array 08:05 Create an Image Object from the MemoryStream 08:55 The Code Below Was Not Modified (Not Entirely True) 09:23 Finalizing the Script & Running the Executable Brien Posey is a bestselling technology author, speaker, and 21x Microsoft MVP. In addition to his ongoing work in IT, Posey has trained as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. For daily news, analysis, opinions and how-to’s about the IT industry, visit us at ITPro Today: https://www.itprotoday.com/ Music by AudioCoffee on Pixabay *POWERSHELL SCRIPT EXAMPLES* # Depend.ps1 Script Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing   $Form = New-Object system.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size(800,600) $Form.StartPosition = "CenterScreen"   $JpgPath = "C:\Scripts\Portable\Logo.jpg" $JpgImage = [System.Drawing.Image]::FromFile($JpgPath)   $PictureBox = New-Object System.Windows.Forms.PictureBox $PictureBox.Size = $JpgImage.Size $Form.Controls.Add($PictureBox) $PictureBox.Image = $jpgImage   $Form.ShowDialog () # Conversion.ps1 Script *Convert the JPG file to a Base64 encoded string* $Base64Image = [Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\Scripts\Portable\Logo.jpg"))   *Output the image* $Base64Image | Out-File "C: \scripts\portable\logo.txt" # Empty Portable.ps1 Script *Base64 encoded image string* $Base64Image = @" Your Base64 Encoded String Here "@   *Decode the Base64 string to a byte array* $ImageBytes = [Convert]::FromBase64String($Base64Image)   *Create a MemoryStream from the byte array* $MemoryStream = New-Object System.IO.MemoryStream $MemoryStream.Write($ImageBytes, 0, $ImageBytes.Length) $MemoryStream.Seek(0, [System.IO.SeekOrigin]::Begin)   *Create an image object from the MemoryStream* $JpgImage = [System.Drawing.Image]::FromStream($MemoryStream)   *The Code Below Was Not Modified* Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing   $Form = New-Object system.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size (800,600) $Form.StartPosition = "CenterScreen"   $PictureBox = New-Object System.Windows.Forms.PictureBox #powershellscripting #powershellscript #powershell

Download

0 formats

No download links available.

Make Portable PowerShell EXEs Without External Dependencies | NatokHD