How to Manually Stop a Long Script Execution in PowerShell

  1. Common Causes of Script Termination in PowerShell
  2. Manually Stop a Terminated Script in PowerShell
How to Manually Stop a Long Script Execution in PowerShell

When executing scripts, especially ones with long lines of codes with long execution times, there will be times that scripts will seem to terminate and be in a hung state. This article will discuss common causes why our script execution stops midway and how we can stop a long-running PowerShell process.

Common Causes of Script Termination in PowerShell

Here are a few common causes of why our script terminates while running inside our PowerShell environment:

  1. Lack of Resources

    The lack of resources on a machine is one of the common causes of why PowerShell execution stops mid-way. This situation happens especially if your device or server serves multiple jobs or functions and has fewer resources than it can handle.

    Priority processes are sometimes executed first, and interruptions occur, thus halting the script midway. However, sometimes the interpreter couldn’t resume from its breakout line as any connections established may have already been closed or terminated, leading us to our following example.

  2. Connection Timeout

    This situation usually happens when our script includes a database or remote connections to other locations like, for example, a server. Our script will take too long to respond, and sometimes, PowerShell will not be able to determine if the end connection has been closed or not, thus leaving us in a stuck process for quite some time.

  1. Visual Bug

    If we are using PowerShell IDE, we may encounter one of the visual bugs where if we click the blue PowerShell window while it is running, it may not display messages nor reveal its status while the script is running. So technically, the script is still running in the back end, but it may look like the script execution has stopped for quite some time in our monitor.

Manually Stop a Terminated Script in PowerShell

We can stop the script manually by sending an interrupt signal to our interpreter. This applies to PowerShell and most script or programming languages that use the command line.

Here are some of the few examples of Interrupts:

  1. Ctrl+C sends SIGINT to interrupt the application. It usually causes it to abort, but this is up to the application to decide.
  2. Ctrl+Z sends SIGTSTP to a foreground application, effectively putting it in the background, suspended. This is useful if we break out of something like an editor and grab some data we need.

If we use cloud or web-based terminals like Azure Shell or PowerGUI, a simple Refresh with cache using Shift+F5 will work as an interrupt signal.

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell Script