Powershell tip: Wait for a certain time

Page content

Sometimes I write a script where I need To wait until a certain time or for a certain event, like after business hours or when a service is in a running state.

To achieve this I use a do…until loop simply repeating a short wait time until my requirement returns true.

The following snippet will pause until a given time:

do {<br /> Start-Sleep 1<br /> }<br /> until ((get-date) -ge (get-date "6:00 PM"))

This will simply check current time once every second until it’s 6:00 PM.

Depending on how accurate the wait time needs to be, the start-sleep could be set to a higher value. On most occasions it is more than enough to check if the time is right to continue once every one minute or even once every 10 minutes, but waiting for a service to start probably wont take even a minute in total.