I need help with wrapping my PowerShell script into a single executable file (.EXE) so that my client can just double-click it and have everything run without a hitch. The script is designed to create a SharePoint site with specific parameters and relies on modules that require PowerShell 7, such as PnP.PowerShell. Even though PowerShell 7 is installed on the client machines, when I use PS2EXE to compile the script, it defaults to Windows PowerShell 5.1, which breaks the functionality of my script. Ideally, I want the executable to run with PowerShell 7's 'pwsh.exe' instead of the 5.1 version. My goal is to have a self-contained, easy-to-use solution where the client doesn't have to worry about any dependent PS1 files being accidentally deleted. I found a workaround where the EXE calls pwsh.exe to run an external PS1 file, but it feels messy and goes against the simplicity I'm aiming for. Has anyone discovered a cleaner method or alternative that keeps everything streamlined?
5 Answers
While there isn't a perfect solution for compiling PowerShell scripts, wrapping your PS in a C# console app could be a good route. It can provide the straightforward experience you want, and you can leverage any .NET functionality you need.
If feasible, consider modifying your script to check for the PowerShell version and relaunch itself with 'pwsh' if necessary. This way, you can keep it running in the environment it was designed for without having to worry about compilation.
That's a clever idea! I'll explore this approach to make sure it runs smoothly for users.
I understand your frustration. While there's limited support for this, consider just creating a shortcut that points directly to 'pwsh.exe' with your script as an argument. It has worked reliably for others, keeping it simple for users.
Sounds good! I'll try that instead of overcomplicating things. Fingers crossed it’s straightforward enough for my non-technical client!
You might look into tools like PowerShell Studio from SAPIEN. It can create .EXE files from your scripts which might simplify deployment. It's been a long time since I used it, but worth checking out!
Another option is to create a batch file that calls your PowerShell script. While this might not give you the EXE packaging, it would provide a double-click experience for your users and avoid potential malware flags that come with EXE files.
I haven't done much with C#, but if it's easier in the long run, I might look into it. I'm already good with documentation, so I might manage to pull it off!