Tip: Powershell loop freezes

    This is a small tip about an issue that does not happen to a lot of people and not too often but it happens and when it does you don’t even know hot to start troubleshooting it. The issue sounds like this: you are running a script or some commands and the execution just got to a loop. You look at the screen, everything is OK, you click around and then let the script finish it’s job. The problem is that you notice after some time that the loop freezes; no activity is seen anymore. In the image below you can see a script that enumerates numbers from 1 to 10000 that stopped during run time.

Powershell loop freezes

Powershell loop freez

    In this moment you may be asking yourself where did you go wrong with the script, why is the loop freezing without any warning or error. If this happened to you I am here to say that you are not the only one that this happened to and that your script or code is not the fault. The fault is the mouse, or more precisely the way you use it.

    When clicking around like I mentioned, it is very likely that you clicked on the Powershell console. If you do this, the Powershell console will wait for some input from the keyboard before going on with executing what it is executing. Thsi in turn means that your loop freezes. To fix it in that moment just press a key on the keyboard. To fix it forever just be careful when clicking in the PS window to not leave it waiting for input.

    There are 2 easy ways to see when Powershell is waiting: the title window has a Select in front and a light yellow cursor will be visible on the console (like in the image below near the number 9146).

Powershell window loop freezes

Powershell window with froze loop

    In this case after I press a key the cursor will dissapear and the window title will be back to the normal Windows PowerShell.

powershell normal window without loop freezes

Normal mode Powershell Window

    This issue is not specific to loops. If you run a couple of chained commands and click in the Powershell window during execution, the command that comes after the moment you clicked will be paused.


1 Comment

  1. Devon

    I have this happen to me all the time, And I’ve had a growing concern about powershell being reliable for long tasks. Now I’ll be on the lookout for this. Thank you


Leave a Comment

Your email address will not be published. Required fields are marked *