none
How to close active PowerShell Window, upon script completion. RRS feed

  • Question

  • Hi All,

    I searched through the forums, and didn't find this specific question.  My apologies if the answer is out there.

    I've written a script that launches a PS1 via a shortcut on my desktop.  This is a GUI app, so the next thing I did was figure out how to hide the shell during it's execution.  The final part thats missing is how to exit the hidden shell window upon completion.

    Currently, I'm unhiding the window so that I can type "EXIT" manually.  This is fine for me, but for other users who may use this script, I'd prefer that they never see the command window, except maybe very briefly during the launch of the script.

    The only way I've found to do this involves querying PS for the Window Title, and then executing Stop-Process; however the script in quesion is an Exchange 2010 script which will not let me modify the Window Title from "Machine: SERVERNAME.Domainname.com".

    If anyone has any ideas, or needs any more info - I would appreciate any help you can give me.

    Thanks!

    Jay

    Wednesday, October 19, 2011 5:31 PM

Answers

  • $PID is an automatic variable that holds the process ID of the session.

    stop-process -Id $PID


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by MS_Convert Wednesday, October 19, 2011 5:57 PM
    Wednesday, October 19, 2011 5:55 PM
  • Have you tried using $PID?

      Pass it to the called script as a parameter, and have the called script use that to identify the process that needs to be stopped.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by MS_Convert Wednesday, October 19, 2011 5:57 PM
    Wednesday, October 19, 2011 5:42 PM

All replies

  • Have you tried using $PID?

      Pass it to the called script as a parameter, and have the called script use that to identify the process that needs to be stopped.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by MS_Convert Wednesday, October 19, 2011 5:57 PM
    Wednesday, October 19, 2011 5:42 PM
  • Sorry, I re-read my first line and realized it was not clear.

    The shortcut on my Desktop launches the script (and I hide it's shell window).  That same script then needs to close it's own hidden shell window when a user clicks the "close" button on my GUI.  I've already put the code together that closes the form, but once the script completes, I'm left with a PowerShell window that just sits there until I type "exit".

    Is there a way to determine, via the script, what PID itself is running as?  In theory: Get-Process "Self"?

    I'm having trouble trying to put this into words, so I apologize.

    Thanks,
    Jason

     

     

    Wednesday, October 19, 2011 5:51 PM
  • $PID is an automatic variable that holds the process ID of the session.

    stop-process -Id $PID


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    • Marked as answer by MS_Convert Wednesday, October 19, 2011 5:57 PM
    Wednesday, October 19, 2011 5:55 PM
  • So it is me that was not understanding.  That's exactly what I was looking for!  Thank you so much!

     

    Jay

    Wednesday, October 19, 2011 5:57 PM
  • It helps me also

    mustaque

    Thursday, February 13, 2014 10:03 AM
  • Yuuup, that does it u are the man!
    Friday, July 18, 2014 7:37 PM
  • Thanks much. It did the trick for me too.

    Abhi..

    Friday, September 5, 2014 11:07 AM
  • Thanks

    stop-process -Id $PID

    Worked for me

    Tuesday, July 24, 2018 7:52 PM