2

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?

4
  • How are you invoking the script? Commented Aug 26, 2013 at 21:22
  • Also, how are you calling the C# program, is it waiting for the application to complete? Commented Aug 26, 2013 at 21:46
  • @DanielMann I think powershell.exe is called and the line to invoke the script is executed in the console. That's just a guess though. We did manage to close the window by using the $PID number and using powershell to close that number. Commented Aug 27, 2013 at 13:02
  • @AthomSfere The C# program is passed like this .\ProductiondataImportNew.exe I am guessing that powershell is waiting for the application to finish because when I run the script from my computer I can't enter any commands into powershell until the C# program is finished. Commented Aug 27, 2013 at 13:04

3 Answers 3

2

Have you tried -NonInteractive mode while invoking power

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

Sign up to request clarification or add additional context in comments.

1 Comment

No I haven't but I'll suggest it. My boss is using $PID to get the id of the powershell console and closing the powershell application from the $PID. It's a very brute way of doing this but it works.
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.