Power Shell Script need a Delay/Pause

Thomas Grassi
Thomas Grassi used Ask the Experts™
Windows 2016 Datacenter

Power Shell   5.1.14393.2430

Power Shell Script need a Delay before Remainder of Code runs  my method does not seem to be working the script came from a windows 2008 server for exchange 2010 this code worked fine on 2008 server but not so well on 2016 server  

I have a power script for my exchange server 2016

$timeout = new-timespan -Minutes 10
$ticker = [diagnostics.stopwatch]::StartNew()

while ($ticker.elapsed -lt $timeout) {    
       if (Get-MailboxExportRequest -mailbox journalman | Where {$_.Status -eq “Completed”}) {       
              search-mailbox -identity journalman -searchquery "Received:$Filter"  -deletecontent -force  | out-file $LogFolder\$LogFile -append   
              search-mailbox -identity spamman -searchquery "Received:$Filter"  -deletecontent -force  | out-file $LogFolder\$LogFile -append  
       start-sleep -seconds 60

Open in new window

The above code I need to wait for about 3 to 5 minutes before it runs because of code that is above this may still be running.
The code is two get-mailboxexportrequests commands
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Exchange Engineer
Distinguished Expert 2018
Instead of the timer in the script you can try this:

Start-Sleep -Seconds 300

This causes everything after the timer to stop processing and for 5 minutes of whatever you decide. You can only use seconds and milliseconds. I tested with the below example on mailboxes I have:

Set-Mailbox -Identity guns -DeliverToMailboxAndForward:$true -ForwardingAddress workouts
Start-Sleep -Seconds 300
Set-Mailbox -Identity guns -ForwardingAddress $null -DeliverToMailboxAndForward:$false

I had the script set a forward, and then it stopped processing for 5 minutes, and then it continued processing the script and removed the forward. Just comment out your timespan timer and use this and see how it works.
MichelangeloSystem Administrator / Postmaster


you are already using in your code
Start-sleep -seconds 60
just use the same approach to introduce a delay whenever you need it.
Thomas GrassiSystems Administrator


The Sleep command will work this runs every Monday.

If I run into a problem will post again
timgreen7077Exchange Engineer
Distinguished Expert 2018

sounds good.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial