Waiting for open windows to close

I have written a powershell script that executes CCleaner to clean the system drive of garbage files. As you most likely know for third party apps like CCleaner to clean internet cache, history etc for web-browsers the browsers need to be closed. So the first line in the script is

(get-process | ? { $_.mainwindowtitle -ne "" -and $_.processname -ne "powershell" } )| stop-process

It works as expected and closes all active windows except for Powershell, however it doesn’t close them fast enough before CCleaner starts. How can I pause the script until this line completes executing before continuing?

I have tried using wait-process as described in this link, however it is apparently won’t work with the line above

I’d rather not use something to pause the script for X seconds, in case a user has a lot of open apps, one or more apps takes a while to close or it is just a slow system, I also don’t want to pause the script any longer then necessary.

Anybody have any ideas or suggestions?

Welcome to the forum. :wave:t4:

As you can see in your post your code is messed up because you did not format it as code. So when you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

Wait-Process will wait until a process stops. It will not stop them. So you simply combine both cmdlets

$ProcessList = 
    Get-Process |
        Where-Object { $_.mainwindowtitle -and $_.processname -ne 'powershell' }
$ProcessList | Stop-Process
$ProcessList | Wait-Process

I have edited my original post using the preformatted text button.

I have added the suggested code to my script. However I have a new problem

When I added the suggested code to my script and tested it, I had a program running that this code could not / would not close. The error presented says that a process with the name ‘X’ couldn’t be found. I am however able to use taskkill to close to program. I found the code below to run Taskkill to close it, but the program will not close unless command prompt is run as administrator.

$app = "GVEdgeRecording.exe"
$path2exe = "C:\GVEdgeRecording\GVEdgeRecording.exe"
$running = tasklist.exe | findstr -i $app
if ($running)
{Cmd /c taskkill /IM $app}

Great. :+1:t4:

I’d say that’s feature by design and it is not a PowerShell issue anymore. I assume you could kill the process even without taskkill when you run the script in an elevated PowerShell session.

But if the process you want to close needs administrative rights to get closed you’re pretty much out of luck. You cannot bypass the UAC with PowerShell. Some programs have an API they expose to get controlled by a command line call. If you’re lucky your GVEdgeRecording.exe has something like this and you don’t need to kill the process forcibly.

Another quirky option would be to use a tool like AutoIt or AutoHotkey to actually click the close button of the graphical interface of the program. But that’s not a PowerShell issue anymore either. :wink: