Long Running Powershell Script Freezes

powershell script stops running
powershell script randomly stops
powershell script hangs until keypress
powershell stuck enter
powershell freezes windows 10
powershell output hangs
powershell console hangs
powershell freezes until keypress

Dear PowerShell users of SO,

We are using a long running PowerShell script to perform a lot of small operations that can take an extremely long amount of time. After about 30 minutes the scripts froze. We were able to get the scripts to start running again by pressing Ctrl-C which caused the scripts to resume execution instead of killing the process.

Is there some sort of script timeout or mechanism that prevents long running scripts within PowerShell?

Thank you for your help.

I had this problem due to a bad habit I have. If you select a little bit of text inside a console powershell, scripts logs freeze. Make sure nothing is selected after launching a big script :)

[SOLVED] Powershell script hangs when run from task manager, The ps1 script I created runs perfectly when it's run from inside powershell. I've also created a batch file during my development for setting up a scheduled task to� Opening a cmd that runs powershell that runs a script is not a good idea. There are plenty options that would be better. For your question: If you run a script that renders a GUI and if you run anything that blocks the thread it causes your GUI to freeze since every action you make waits for the blocking function to finish.

I have not had any responses to this question after a really long time. I am going to assume it was something wrong with my script or a child thread freezing. If this is not the case please answer.

Powershell hangs?, Microsoft Technical Communities � Microsoft Virtual Academy � Script Center � Server and The powershell console will just hang and will require you to hit enter (or maybe Powershell pauses execution if something is selected. I as i understand it powershell can sometimes take a long time to open� The goal is that it should feel just like running a PowerShell script. If you notice anything that doesn't seem right to you, please open an issue and we'll look into it. If you haven't seen this before, there are some useful PowerShell code golf tips here.

try to add percentage calculation in your script.. so you can identity that how much time it would take to complete...

Tip: Powershell loop freezes – About Windows Server, Tip: Powershell loop freezes The issue sounds like this: you are running a script or some powershell normal window without loop freezes And I've had a growing concern about powershell being reliable for long tasks. It pops up about 20 percent of the time, and is accompanied by a box to check “stop script.” My computer is frozen until the message goes away (a minute or so).

Try my kill timer script. Just change the $ScriptLocation variable to the script you want to run. That script will then run as a background job while the current windows keeps track of the timer. After the time expires the current window will kill the background job and write it all to logs.

    Start-Transcript C:\Transcriptlog-Cleanup.txt #write log to this location
$p = Get-Process  -Id $pid | select -Expand id  # -Expand selects the string from the object id out of the current process.
Write-Host $p
$BJName = "Clean-up-script"   #Define Background job name

$startTime = (Get-Date) # set start time
$startTime
$expiration = (Get-Date).AddMinutes(2)#program expires at this time
# you could change the expiration time by changing (Get-Date).AddSeconds(20) to (Get-Date).AddMinutes(10)or to hours or whatever you like


#-----------------
#Timer update function setup
function UpdateTime
   {
    $LeftMinutes =   ($expiration) - (Get-Date) | Select -Expand minutes  # sets minutes left to left time
    $LeftSeconds =   ($expiration) - (Get-Date) | Select -Expand seconds  # sets seconds left to left time


    #Write time to console
    Write-Host "------------------------------------------------------------------" 
    Write-Host "Timer started at     :  "  $startTime
    Write-Host "Current time         :  "  (Get-Date)
    Write-Host "Timer ends at        :  "  $expiration
    Write-Host "Time on expire timer :  "  $LeftMinutes "Minutes" $LeftSeconds "Seconds"
    Write-Host "------------------------------------------------------------------" 
    }

    #get background job info and remove the it afterwards + print info
function BJManager   
    {
       Receive-Job -Name $BJName  #recive background job results
       Remove-Job -Name $BJName -Force #remove job
       Write-Host "Retrieving Background-Job info and Removing Job..."
    }
#-----------------
$ScriptLocation = "C:\\Local-scripts\Windows-Server-CleanUp-Script-V2.4(Beta).ps1"  #change this Var for different kind of script locations

Start-Job -Name $BJName -FilePath $ScriptLocation #start this script as background job
# dont start job in the loop.
do{   #start loop

   Write-Host "Working"#start doing other script stuff
   Start-Sleep -Milliseconds 5000  #add delay to reduce spam and processing power
   UpdateTime #call upadate function to print time

   Get-Job -Name $BJName  | select Id, State ,Location , Name
        if((Get-Job).State -eq "Failed")
            {
                BJManager
            }
        elseif((Get-Job).State -eq "Completed")
            {
                BJManager
            }

 }
until ($p.HasExited -or (Get-Date) -gt $expiration) #check exit time

Write-Host "Timer Script Finished"
Get-Job -Name $BJName  | select Id, State ,Location , Name
UpdateTime

BJManager

Start-Sleep -Milliseconds 5000 #give it some time to write to log
Stop-Transcript
Start-Sleep -Milliseconds 5000 #give it some time to stop the logging before killing process
if (-not $p.HasExited) { Stop-Process -ID $p -PassThru } # kill process after time expires

Running powershell script freezes / Problems / Discussion Area , I am attempt to run a powershell script to install vcpkg and it hangs in appveyor web app. Using RDP I attempted to run that same script again� We are using a long running PowerShell script to perform a lot of small operations that can take an extremely long amount of time. After about 30 minutes the scripts froze. We were able to get the scripts to start running again by pressing Ctrl-C which caused the scripts to resume execution instead of killing the process.

Breaking PowerShell out of a hang : PowerShell, Sometimes PowerShell will hang during the execution of a command. But, depending on the machine you're querying, PowerShell IDE MIGHT get stuck with the message "Running script / selection. Perhaps building a long string it's ok. Enable Web Scripts. Check whether Internet Explorer has blocked the Active scripting, Active X, and Java. If they are blocked, the webpage will not load because they are the only reason for displaying a webpage.

Running PowerShell script hangs � Issue #73510 � microsoft/vscode , When I run this script in VSCode via F5 or ctrl+F5, VSCode just hangs. However, when I tried Code Runner, the script runs just fine. Also, while� I have a script that we use to check services on remote servers, and email if a service is down. However, if a server happens to be down while the script is running, it can cause the script to run for a very long time. Is there any way to speed up the process? I would like the script to continue until completion. Any help would be greatly

PowerShell just Hangs, Anyone else have problems when running a PowerShell script, where the script just hangs in one spot until you hit Enter or something on the console window? I have tested in past, left running the application for days and it was fine, now its running the application and this powershell script. however after 12h to 18h sometimes even a day or so the computer freezes and i cannot RDP or ping or anything, completely freezes - so it is the powershell script causing it.

Comments
  • To anyone facing the same issue, check this out to disable this "feature"
  • I had that happen all the time! Thanks for adding this.
  • Actually, I'm having behavior that sounds the same as yours. I have a template that grabs a list of computers on the domain from ActiveDirectory and performs some given task (usually a get-wmiobject). But even on my simplest test script that just does Test-Connection, it hangs on several machines. I also notice that it is the same machines, and that if I reboot the problem machine, the problem often goes away. I'll keep poking around, and I'll post here if I find a definite answer.
  • I am not sure whether this was the eventual reason but I have noticed that some executables that need console input will simply not work in the ISE (there is no way to provide extra input). I don't think this was the issue here and it probably was the script I had written.
  • In my test script, though, it is really just a wrapper to apply test-connection to all domain computers. I don't believe Test-Connection should ever require further input, so I don't know why it's hanging. Also, my script runs each as a job, but it still halts on particular machines without errors--it can even run all night. But if I press ctrl-c, it continues on without problem...
  • In general, do not place links in an answer unless the link contains relevant info for the question. You can link to your blog in your profile.