Hướng dẫn thêm pause (tạm dừng) vào script PowerShell.
Có nhiều cách thêm pause/wait command. Dưới đây các cách kèm ví dụ.

Cách thêm Pause vào PowerShell Script
- Mở PowerShell ISE từ Start menu
- Mở script PowerShell cần thêm pause
- Thêm dòng: start-sleep -Seconds 5 (thay 5 bằng số giây muốn pause) tại vị trí cần pause

- Ví dụ trên thêm wait command ở dòng 3
- Lưu script → Chạy script
Cú pháp Start-Sleep:
Start-Sleep -seconds
Start-Sleep -milliseconds
Ví dụ
Start-Sleep -seconds 1.5 # Ngủ 1.5 giây
Start-Sleep -seconds 5 # Ngủ 5 giây
Start-Sleep -seconds 15 # Ngủ 15 giây
Start-Sleep -milliseconds 1.5 # Ngủ 1.5 ms
Start-Sleep -milliseconds 5 # Ngủ 5 ms
Start-Sleep -milliseconds 15 # Ngủ 15 ms
Cách thêm Pause có Countdown (đếm ngược)
Hiển thị countdown khi pause. Có thể ngắt bằng phím bất kỳ để script tiếp tục.
- Mở PowerShell ISE
- Mở script cần thêm pause
- Thêm dòng: timeout /t 15 (thay 15 bằng số giây) tại vị trí cần pause

- Lưu script → Chạy script
Giao diện timeout:

Phương pháp thay thế
Thay timeout /t 15 bằng code sau:
1..60 | ForEach {
Start-Sleep -s 1
Write-Progress -activity "Timer Started: " -Status $_
}
Hiển thị countdown timer khác (mỗi giây cập nhật progress). Thay 60 bằng thời gian muốn đếm.

Cách thêm Pause với Popup Message
Hiển thị thời gian pause trong popup window.
- Mở PowerShell ISE
- Mở script
- Thêm code từ bài viết này
- Lưu → Chạy script
Giao diện popup:

Làm PowerShell Sleep với Progress Bar
Cách yêu thích: Sleep kèm progress bar.
- Mở PowerShell ISE
- Mở script
- Thêm code sau:
For ($i=0; $i -le 100; $i++) {
Start-Sleep -Milliseconds 20
Write-Progress -Activity "Sleeping For 20 Seconds" -Status "StatusString" -PercentComplete $i -CurrentOperation "CurrentOperationString"
}
- Thay số 20 bằng giây muốn sleep
- Thay text "Sleeping For 20 Seconds" tùy ý
- Lưu → Chạy script
Giao diện progress bar:
