Power Shell Script need a Delay/Pause

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
LVL 23
Thomas GrassiSystems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

timgreen7077Exchange EngineerCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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 AdministratorAuthor Commented:
The Sleep command will work this runs every Monday.

If I run into a problem will post again
timgreen7077Exchange EngineerCommented:
sounds good.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.