You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell、批处理或VBS实现电脑两次重启?

解决Windows两次自动重启的问题

原脚本失效原因

你最初的脚本执行第一次Restart-Computer -Force后,系统会立即重启,当前PowerShell进程直接终止,后续的Start-Sleep和第二次重启命令根本没有机会运行,所以只完成一次重启。

可行解决方案

方案一:注册表RunOnce项(轻量高效)

利用Windows的RunOnce注册表项,让系统在第一次重启后自动执行第二次重启的逻辑,无需额外计划任务。
编写如下PowerShell脚本(必须以管理员身份运行):

# 配置RunOnce,系统重启后延迟5分钟执行第二次重启
$runOnceKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"
Set-ItemProperty -Path $runOnceKey -Name "DoubleRestart" -Value 'powershell.exe -NoProfile -Command "Start-Sleep -Seconds 300; Restart-Computer -Force"'

# 执行第一次重启
Restart-Computer -Force
  • 原理:RunOnce中的命令会在系统下次启动后自动执行一次,执行后自动删除该注册表项
  • 注意:必须管理员权限,否则无法修改HKLM路径下的注册表

方案二:Windows计划任务(稳定可控)

  1. 编写第二次重启的脚本SecondRestart.ps1:
Restart-Computer -Force
  1. 创建计划任务:
    • 触发条件:选择「系统启动后」,延迟5分钟(对应你需要的300秒)
    • 操作:启动程序,选择powershell.exe,参数填-NoProfile -ExecutionPolicy Bypass -File "C:\路径\SecondRestart.ps1"
    • 安全选项:勾选「不管用户是否登录都要运行」,并勾选「使用最高权限运行」
  2. 手动执行第一次重启:Restart-Computer -Force
    系统重启后,计划任务会按时触发,执行第二次重启。

方案三:C#编写EXE(权限可控,适合复杂场景)

用C#编写一个控制台程序,通过注册表标记区分首次/二次运行,配合RunOnce实现两次重启,编译后直接运行即可:

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Threading;

namespace DoubleRestartTool
{
    class Program
    {
        static void Main(string[] args)
        {
            const string regMarkerPath = @"SOFTWARE\DoubleRestartMarker";
            bool isSecondRun = false;

            // 检查是否是第二次运行
            using (var regKey = Registry.LocalMachine.OpenSubKey(regMarkerPath, true))
            {
                if (regKey != null && regKey.GetValue("FirstRestartCompleted") != null)
                {
                    isSecondRun = true;
                    // 清理标记
                    regKey.DeleteValue("FirstRestartCompleted");
                    Registry.LocalMachine.DeleteSubKey(regMarkerPath);
                }
                else
                {
                    // 第一次运行:创建标记,设置RunOnce让程序重启后自动启动
                    Registry.LocalMachine.CreateSubKey(regMarkerPath).SetValue("FirstRestartCompleted", "1");
                    string exePath = Process.GetCurrentProcess().MainModule.FileName;
                    Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce")
                        .SetValue("DoubleRestartTool", $"\"{exePath}\"");

                    // 执行第一次重启
                    Process.Start(new ProcessStartInfo("shutdown", "/r /f /t 0") { CreateNoWindow = true });
                    return;
                }
            }

            // 第二次运行:延迟5分钟后重启
            Thread.Sleep(300000);
            Process.Start(new ProcessStartInfo("shutdown", "/r /f /t 0") { CreateNoWindow = true });
        }
    }
}
  • 编译后以管理员身份运行即可,程序会自动处理两次重启的逻辑
  • 优点:无需依赖PowerShell执行策略,权限可控性更强

内容的提问来源于stack exchange,提问作者Jose Serra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 03:27:32