PowerShell tutorial – exploring Start-Sleep CMDLET

Hi All,

Greetings for the day!!! Continuing the PowerShell tutorial

Today exploring one of the important CMDLET – Start-Sleep

Details / Use Case

  • In one of the my application I was executing PowerShell script against SharePoint online
  • PowerShell script was containing CMDLETs from SharePoint online module and looping through site collections
  • Suddenly script started throwing and 429 error – Too Many Requests
  • This means we are getting throttled in SharePoint online
  • So the CMDLET Start-Sleep came to rescue 🙂

SharePoint Online uses throttling to maintain optimal performance and reliability of the SharePoint Online service. Throttling limits the number of API calls or operations within a time window to prevent overuse of resources

SYNTAX

Start-Sleep -Seconds <int>

Start-Sleep -Milliseconds <int>

fig : PowerShell Tutorial - exploring "Start-Sleep" CMDLET
fig : PowerShell Tutorial – exploring “Start-Sleep” CMDLET
  • The Start-Sleep cmdlet suspends the activity in a script or session for the specified period of time
  • We can use it for many tasks, such as waiting for an operation to complete or pausing before repeating an operation
  • In my PowerShell script I used this CMDLET – Start-Sleep in between SharePoint online CMDLETs and 429 error resolved

Demo

  • Example 1 – Demonstrating pause of 2 seconds in between two CMDLETs
#getting current time
$currentTime = Get-Date -DisplayHint Time

Write-Host "Current time is - " $currentTime
Write-Host
Write-Host lets wait for 2 seconds
Write-Host

#pausing for 2 seconds
Start-Sleep -Seconds 2

#getting time after 2 seconds
$timeAfter2Secs = Get-Date -DisplayHint Time

Write-Host "After waiting 2 secs time is - " $timeAfter2Secs

Output : 

Current time is -  9/2/2023 11:35:34 PM

lets wait for 2 seconds

After waiting 2 secs time is -  9/2/2023 11:35:36 PM

fig : PowerShell Tutorial - exploring "Start-Sleep" CMDLET
fig : PowerShell Tutorial – exploring “Start-Sleep” CMDLET

REFERENCES

Thanks for reading! If you think this is useful, kindly please like and share! HAVE a FANTASTIC LEARNING ! LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.