Compiled PowerShell 7 Script Missing PackageManagement Module


All of the commands for working with package providers and modules are failing when my script is compiled as an EXE for x64 Windows with .NET 6. When I look at the modules that are bundled by Merge-Script, I only see the following:

Microsoft.PowerShell. Diagnostics
Microsoft.PowerShell. Utility

How I can force Merge-Script to include the necessary commands for my final exe to Import and Install Modules and Package Providers?

Thank you.

Here is a very simple example that demonstrates the problem.



Install-Module command not recognized:

Merge-Script doesn’t include modules without Import-Module so you will want to try to import those at the top of your script. The modules that are being bundled are the default set of modules.

Import-Module PowerShellGet
Get-Command -Module PowerShellGet

It’s a bit of a chicken and an egg problem with PowerShellGet all the time since you can use PowerShellGet to install PowerShellGet etc. Are you trying to install a new version of PSGet with this test script?

Cheers. I thought I had to do Install-Module first since the source files for the modules wouldn’t be present on the end-user’s machine to import, but once I did Import-Module PowerShellGet then all subsequent uses of Import-Module just worked. So it appears you parse that during compilation, retrieve the module source files, and then zip them up into the final exe as embedded resources. Pretty cool. Marked your answer as the solution.