Powershell Tutorial Online


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

Start-Sleep cmdlet is

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

Sleep for 5 seconds

    PS C:\> Start-Sleep -s 5

Sleep for 300 miliseconds

    PS C:\> Start-Sleep -m 300

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.

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

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.

$comp = Get-ADComputer -filt *                                      #credentials and AD filter
$cred = Get-Credential PC\administrator                             #create credentials variable
$session = New-PSSession -cn $ -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 $