I have few powershell script in orders. Can I convert the powershell scripts into one exe file ? I need set up it to run in order. Example, once installing with first script, second script need be installed and followed by 3,4,5 scripts.
1 Answers
In the simplest case, you can use the following approach to merge your scripts into a single script that you can then package as an executable:
$scripts = 'script1.ps1', 'script2.ps1', 'script3.ps1'
(Get-Item $scripts | ForEach-Object {
"& {{`n{0}`n}}" -f (Get-Content -Raw $_.FullName)
}) -join "`n`n" > 'combined.ps1'
Note that this is a simplistic, but extensible approach: As written, there is no support for parameters, and no error handling: the respective contents of the original scripts are simply executed (&
) in sequence, as script blocks ({ ... }
).
You can compile the combined script, combined.ps1
to an executable, say, combined.exe
, as follows, using the PS2EXE-GUI project's ps2exe.ps1
script (an updated and more fully featured version of the popular original, but obsolescent PS2EXE project).
# Create a PSv2-compatible executable.
# Omit -runtime20 to create an executable for the same PowerShell version
# that runs the script.
ps2exe -inputFile combined.ps1 -outputFile combined.exe -runtime20
Caveat: Generally, running the resulting executable requires the executing machine to have PowerShell installed, but due to targeting -runtime20
- in an effort to be v2-compatible - the .NET Framework 2.0 CLR must also be installed (note that it also comes with .NET Framework 3.5), which is no longer true by default in recent versions of Windows.

- 382,024
- 64
- 607
- 775
-
1it may be worth adding a version of this answer here, I think your answer if far more detailed and likely of better use - https://stackoverflow.com/questions/48338017/convert-powershell-script-to-exe – Matthew Dec 12 '19 at 00:43
-
Thanks, @Matthew, but this answer is too specifically tailored to this question; generalizing it in order to make it useful as an answer to the linked question would require quite a bit more effort. – mklement0 Dec 12 '19 at 02:16
-
1Thanks @mklement0, I have created a question here: https://stackoverflow.com/q/59993465/139212 – MattB Jan 30 '20 at 19:48