×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Waiting for open windows to close

Waiting for open windows to close

Waiting for open windows to close

(OP)
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

CODE --> powershell

(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?

RE: Waiting for open windows to close

Instead of piping all the process to the stop command, you could put them into a variable and then loop through them to stop. Wait a bit and then check if they are stopped or still running.

CODE -->

$processes = get-process | ? { $_.mainwindowtitle -ne "" -and $_.processname -ne "powershell" }

#Generalized (I didn't test this, more of a concept)
$loop_count = 0
while ($processes -AND $loop_count -lt 5)  #<< Check to make sure $processes is valid otherwise use something like $processes.Count -gt 0 
{
foreach ($process in $processes)
{# stop process}
# wait like 2 seconds or some timeframe
# get processes, again, to confirm stoppage
$processes = get-process | ? { $_.mainwindowtitle -ne "" -and $_.processname -ne "powershell" }
$loop_count ++
} 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close