ProfGhost
ProfGhost

Reputation: 359

Waiting for an process to quit in PowerShell

Is there a possibility to wait for an process to quit, without it needs to running? I know there is the keyword WaitForExit, but to use this the process needs to run.

My second question is, if there is a possibility to use an else-Statement in an while loop. Tried it already, but it always said that there isnt an function called else.

Upvotes: 0

Views: 716

Answers (4)

msKing
msKing

Reputation: 27

You can use wait-process cmdlet. Check link for details http://ss64.com/ps/wait-process.html

Example: wait-process -name notepad.exe

Upvotes: 0

Vasili Syrakis
Vasili Syrakis

Reputation: 9611

Do Until

Do {
    Sleep 5
} Until (Get-Process iexplore);

Will wait until iexplore is found

While

While (Get-Process iexplore) {
    Sleep 5
}

Will wait until iexplore is no longer running


Else after while

You cannot use an else statement after a while loop.

It needs to come after an if.

Upvotes: 1

cubitouch
cubitouch

Reputation: 1937

Does it seems to fits your needs (1st question) ? http://technet.microsoft.com/library/hh849813.aspx

Upvotes: 0

Richard
Richard

Reputation: 109140

if there is a possibility to use an else-Statement in an while loop.

If you mean something like:

while (cond) {
} else {
}

?

Then NO. (how would the content of the else block be any different to code immediately following the while block?)

Is there a possibility to wait for an process to quit,

Yes. There are different ways of doing this, depending on the nature of the target process. Is it one created by the same script? Is the same session? A service? Or just an arbitrary process?

Upvotes: 0

Related Questions