Close a powershell window when called from a batch file RRS feed

  • Question

  • Hi all,

    I have a batch file that calls a powershell script to export mailboxes to pst files. The script is scheduled and runs fine, but when finished the powershell window stays open. I have tried exit commands, searched high and low, but all suggestions are to no avail.

    Is there anyone out there who can actually help?



    Wednesday, December 20, 2017 3:52 AM

All replies

  • Why are you using a batch file.  Just run the script as a task.

    Program: powershell.exe
    Arguments: -file c:\myscript.ps1


    • Edited by jrv Wednesday, December 20, 2017 3:59 AM
    Wednesday, December 20, 2017 3:58 AM
  • Hi there and thanks for the reply...The script has to run under Exchange shell in order to execute the export-mailbox command. The only way I found, is as below:

    PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; . 'C:\Users\admin\Pst.ps1'"

    and Pst.ps1 reads:

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

    foreach ($i in (Get-Mailbox)) { New-MailboxExportRequest -Mailbox $i -FilePath "\\\PST\$($i.Alias).pst" }

    I know that the -noexit command keeps the shell open, but I need it to close once finished..

    Wednesday, December 20, 2017 5:12 AM
  • If you use "-noexit" the shell will never close.


    Wednesday, December 20, 2017 5:19 AM
  • That was done to see if the script would run..I can remove it now...So usually when you run a script, it will close automatically when done? Sorry for the stupid question...
    Wednesday, December 20, 2017 5:48 AM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, December 21, 2017 7:02 AM