none
Force powershell.exe console to exit from a script RRS feed

  • Question

  • I have a powershell script that I am running. I would like the script to be able to close the powershell.exe console window that it is running from once the script has finished. I've already tried calling exit and Exit-PSSession, but those only end the script. They do not close the console.
    Saturday, May 5, 2012 7:28 PM

Answers

  • The root of the issue was that I was launching the script from a C# app and waiting for the call to Invoke to return. I switched to executing a Process, cmd.exe, and NOT waiting for standard out and it worked.
    • Marked as answer by dharric72 Tuesday, May 8, 2012 3:06 AM
    Tuesday, May 8, 2012 3:06 AM

All replies

  • Hello

    For me Exit works fine, it closes the current window, in PS console as well as the ISE windows.


    Olaf Helper
    * cogito ergo sum * errare humanum est * quote erat demonstrandum *
    Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Blog Xing

    Saturday, May 5, 2012 8:03 PM
  • EXIT closes the window, but only when the window was opened by the action that started the script, for example, right-clicking a script and selecting "run with powershell".

    I suspect that dharric72 has opened an interactive console, started his script by typing its full name, and wants to have the window close when the script completes.

    It would be a nuisance if EXIT always closed the window in this context, as that is not always what you would want to have happen. Your script could close the window before exiting with a stop-process command, however, a simpler way to accomplish this would be to run your script with a command like this:

        ./myscript.ps1 <parameters> ; exit


    Al Dunbar

    Saturday, May 5, 2012 11:00 PM
  • If I schedule a batch file to run a powershell script, the cmd windows always closes automatically when the PS script has finished running.  I think I must be missing the point?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Sunday, May 6, 2012 3:04 PM
  • Thanks guys for all your posts. Really appreciate the help. Now in my case I am not running the powershell.exe directly from the task scheduler. We have a 64bit windows application that calls "jobs" to run. These jobs could be anything. In my own case the job is C# code that uses the System.Management.Automation namespace to call my powershell script. Again this job runs fine when testing on my local win7 dev machine, but does not return when run on our win 2008 server. The other added complexity here is that I am using the call below to force a switch of my powershell environment to 32bit mode as the work that the script has to do is on an Access 2007 file. As you can see I do call exit at the end.

    if($env:Processor_Architecture -ne "x86")
    {
        write-warning "Running x86 Powershell ...."
        &"$env:WINDIR\syswow64\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
        exit
    }

    Sunday, May 6, 2012 5:56 PM
  • The root of the issue was that I was launching the script from a C# app and waiting for the call to Invoke to return. I switched to executing a Process, cmd.exe, and NOT waiting for standard out and it worked.
    • Marked as answer by dharric72 Tuesday, May 8, 2012 3:06 AM
    Tuesday, May 8, 2012 3:06 AM
  • Here , i am also using cmd with a process, but the windows powershell windows is still asking to close the window.

    Thanks


    Debabrata Ghosh

    Tuesday, January 30, 2018 1:07 PM