Question

I'm currently using PS2EXE to compile my powershell script into an executable, works very well indeed!

My problem is that this script relies on other files/folders. So instead of having these out with the exe I want to also have these files 'wrapped' up into the exe along with the PS script. Running the exe will run the PS script then extract these files/folders and move them out of the exe...

Is this even possible?

Thanks for your help

Was it helpful?

Solution

A Powershell script that requires external files can be self-sustained by embedding the data within. The usual way is to convert data into Base64 form and save it as strings within the Powershell script. At runtime, create new files by decoding the Base64 data.

# First, let's encode the external file as Base64. Do this once.
$Content = Get-Content -Path c:\some.file -Encoding Byte
$Base64 = [Convert]::ToBase64String($Content)
$Base64 | Out-File c:\encoded.txt


# Create a new variable into your script that contains the c:\encoded.txt contents like so,
$Base64 = "ABC..."


# Finally, decode the data and create a temp file with original contents. Delete the file on exit too.
$Content = [Convert]::FromBase64String($Base64)
Set-Content -Path $env:temp\some.file -Value $Content -Encoding Byte

The full sample code is avalable on a blog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top