Powershell-Sleep

How to wait/pause/sleep the script processing in powershell.

Start-Sleep cmdlet is

Syntax:
Start-Sleep [-seconds] int [CommonParameters]
Start-Sleep -milliseconds int [CommonParameters]


Sleep for 5 seconds

Example


    PS C:\> Start-Sleep -s 5
    

Copy and Try it


Sleep for 300 miliseconds

Example


    PS C:\> Start-Sleep -m 300
    

Copy and Try it

There is an alternate way to pause the execution which wait until the user presses a any key.
This can be achieve using the command.

Example


PS C:\> $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")|out-null

Copy and Try it

Note: But this commands works in powershell command shell not in powershell ISE.

Also you can wait on the job to finish with a timeout.

Example


$comp = Get-ADComputer -filt *                                      #credentials and AD filter
$cred = Get-Credential PC\administrator                             #create credentials variable
$session = New-PSSession -cn $comp.name -cred $cred                 #create session

$job = icm -Session $session -ScriptBlock {gpupdate /force} -AsJob  #create job
Wait-Job $job -Timeout 60                                           #wait job for 60 seconds 
Remove-PSSession $session                                           #remove session
Restart-Computer $comp.name                                           

Copy and Try it