Home > database >  proper way to prevent powershell script from closing console immediately after execution
proper way to prevent powershell script from closing console immediately after execution

Time:10-21

I have automated some of my backup routines using power-shell scripts. What can I do to make sure the console stays open for maybe 5 seconds after execution completes so that I can read whatever feedback is provided and verify that there weren't any unexpected errors?

CodePudding user response:

The following will work in most cases, but, as mentioned in this answer, it does not work in Windows PowerShell ISE.

function Pause {
    Write-Host -NoNewLine 'Press any key to continue . . .';
    $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
}

This closely mimics the the PAUSE command in the Windows command prompt. Simply place the following line where ever you want the code to pause for the user to view information.

Pause
  • Related