Created
December 30, 2025 11:02
-
-
Save jonz94/c893388895cca301434d45ad189daefc to your computer and use it in GitHub Desktop.
透過 PowerShell 在 Windows Task Scheduler 上註冊排程:從晚上十二點到早上八點、每隔 10 分鐘自動關機
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 從晚上十二點到早上八點、每隔 10 分鐘的每個時間點 | |
| $timeArray = for ($i = 0; $i -le 480; $i += 10) { | |
| $hours = [math]::Floor($i / 60) | |
| $minutes = $i % 60 | |
| "{0:00}:{1:00}" -f $hours, $minutes | |
| } | |
| # shutdown action, the command is `shutdown.exe /s /t 0` | |
| $action = New-ScheduledTaskAction -Execute "shutdown.exe" -Argument "/s /t 0" | |
| foreach ($timeString in $timeArray) { | |
| # NOTE: Task 名稱不可包含冒號 | |
| $taskName = "於 $timeString 分時將電腦關機" -Replace ":", " 點 " | |
| # run daily at the specified time | |
| $trigger = New-ScheduledTaskTrigger -Daily -At "$timeString" | |
| $null = Register-ScheduledTask -TaskName "$taskName" -Trigger $trigger -Action $action | |
| Write-Host "成功註冊排程: $taskName" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment