Home > OS >  Start new shell not working when the ps1 has become executable with ps2exe
Start new shell not working when the ps1 has become executable with ps2exe

Time:06-04

I am trying to create an executable (.exe) at windows that will perform some actions at first, and then open an interactive shell. In my case the interactive shell I want to be a wsl, but the problem is shown with every type of shells created. For example:

Supposing that we have the file

test.ps1

Write-Host "Hello from my test program"
Invoke-Expression "powershell"

If I run it with the command: ./test.ps1, the outcome will be to print the message and start a new powershell instance. If i run the command: Invoke-ps2exe .\test.ps1 test.exe, and then run the test.exe, the outcome will be:

Hello from my test program
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

but I cannot write anything at the window. Can anyone explain me why this is happening, and how I could overcome this?

CodePudding user response:

Modify your test.ps1 file as follows:

Write-Host "Hello from my test program"
Start-Process -Wait -NoNewWindow powershell.exe

This change ensures that PowerShell's usual interactive host, ConsoleHost, in the newly launched PowerShell session launched from your script doesn't run on top of the simplified host that is built into the .exe files that ps2exe generates, which appears to cause your problems.

  • Related