Powershell window disappears before I can read the error message

95,954

Solution 1

Try with the -noexit switch:

powershell -noexit d:\script\foo.ps1

Solution 2

You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.

  1. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
  2. Per-script Fix: Add a prompt for input to the end of your script file. e.g. Read-Host -Prompt "Press Enter to exit"
  3. Global Fix: Change your registry key to always leave the PowerShell Console window open after the script finishes running.

Here are the registry keys to modify for option #3:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit \"& \\\"%1\\\"\""

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \\\"%1\\\"\""

See my blog for more information and a .reg file that will apply these registry changes automatically.

Solution 3

I've needed this before and usually I didn't want to modify the script (typically for scripts fired off from the Task Scheduler). I just wanted to see what was spit out to console.

All you need to do is just append a Read-Host command after the script invocation e.g.:

PowerShell.exe -command { .\foo.ps1; read-host "Press enter key to continue" }

BTW the problem with using Start-Transcript is that it doesn't capture EXE output. And any form of attempted logging in V1 and even V2 with the standard host will not capture the verbose, debug, progress or warning streams. You can only see these by viewing the associated host window.

One cheesy but effective way to capture all script output (stdout, stderr, verbose, warning, debug) is to use another host like cmd.exe e.g.:

cmd.exe /c powershell.exe "$pwd\foo.ps1" > foo.log

Solution 4

I am generaly fine with scripts autoclosing except when an error occurs, where I need to see the error. Assuming you have not changed $ErrorActionPreference away from the default 'Continue', then for the behaviour I described do this at the end of you script

if ($Error)
{
    Pause
}

Solution 5

A quick and dirty solution is to use CTRL+S to halt the scrolling of the display and CTRL+Q to resume it.

Share:
95,954

Related videos on Youtube

jadero
Author by

jadero

Programmer by trade, boat builder at heart.

Updated on May 25, 2021

Comments

  • jadero
    jadero about 3 years

    When I call a Powershell script, how can I keep the called script from closing its command window. I'm getting an error and I'm sure I can fix it if I could just read the error.

    I have a Powershell script that sends an email with attachment using the .NET classes. If I call the script directly by executing it from the command line or calling it from the Windows Scheduler then it works fine. If I call it from within another script (IronPython, if that matters) then it fails. All scenarios work fine on my development machine. (I really do have to get that "Works on My Machine" logo!) I've got the call to Powershell happening in a way that displays a command window and I can see a flicker of red just before it closes.

    Sorry: Powershell 1.0, IronPython 1.1

    Solution: powershell -noexit d:\script\foo.ps1

    The -noexit switch worked fine. I just added it to the arguments I pass from IronPython. As I suspected, it's something that I can probably fix myself (execution policy, although I did temporarily set as unrestricted with no effect, so I guess I need to look deeper). I'll ask another question if I run into trouble with that.

    Thanks to all for the help. I learned that I need to investigate powershell switches a little more closely, and I can see quite a few things that will prove useful in the future.

  • jadero
    jadero almost 15 years
    -noexit worked great. As I suspected, it's something I can probably figure out myself. If not, I'll be back.
  • Chris B. Behrens
    Chris B. Behrens over 7 years
    Criminy - can I send you a CHECK for this answer? This has been such a pain for us.
  • lordcheeto
    lordcheeto almost 7 years
    Upvoted for answering the question the best, but you should include those registry keys in your comment. "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline."
  • deadlydog
    deadlydog almost 7 years
    Good suggestion @lordcheeto. Done! :)
  • Alex Zhukovskiy
    Alex Zhukovskiy over 5 years
    For some reason window dissappear with NoExit switch. How could it be possible?
  • greybeard
    greybeard about 3 years
    While a somewhat interesting cause of error: What does this do for I can see a flicker of red just before [the PowerShell] closes?