Darren Hoehna
Darren Hoehna

Reputation: 379

PowerShell console won't close after execution of a script

I have a PowerShell script that, as its last instruction, is calling a C# program. This PowerShell script is being run on a Windows server on a scheduler. The problem is that the PowerShell console window that the script is using won't close or go away after the script is done executing.

We need the console window to close or else the scheduler will have multiple PowerShell.exe programs on the task manager.

We have tried adding exit and break but the window still stays up.

Is there any way in a PowerShell script to force the console window to close after a script is done executing?

Upvotes: 2

Views: 12704

Answers (3)

Deadly-Bagel
Deadly-Bagel

Reputation: 1620

Put this at the bottom of the .ps1 file:

Stop-Process -processname powershell

Found this was due to the StartInfo.RedirectStandardInput being set to true, I guess if it's expecting more input it won't close... Setting it to false correctly closes the window after execution.

Upvotes: 0

Lars Truijens
Lars Truijens

Reputation: 43587

By starting the C# program from PowerShell with Start-Process, you can execute it in parallel and don't need to have PowerShell wait for it to finish. You can use -WindowStyle Hidden to make sure the C# program does not create a new Window when started.

Start-Process .\ProductiondataImportNew.exe -WindowStyle Hidden

Upvotes: 1

Mitul
Mitul

Reputation: 9854

Have you tried -NonInteractive mode while invoking power

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -NonInteractive C:\scripts\myscript.ps1

Upvotes: 2

Related Questions