创建脚本文件

# 提升权限运行脚本
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}

# 停止并禁用Windows Update服务
Stop-Service -Name "wuauserv" -Force
Set-Service -Name "wuauserv" -StartupType Disabled

# 检查并定期停止Windows Update服务
while ($true) {
    $service = Get-Service -Name "wuauserv"
    if ($service.Status -eq "Running") {
        Stop-Service -Name "wuauserv" -Force
    }
    Start-Sleep -Seconds 60 # 每分钟检查一次
}

将此代码保存为 DisableWindowsUpdate.ps1

@echo off
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0DisableWindowsUpdate.ps1"

将此代码保存为 RunDisableWindowsUpdate.bat,并确保与 DisableWindowsUpdate.ps1 位于同一文件夹中。

创建任务计划在启动时运行批处理文件

按 Win + R 键打开“运行”对话框,输入 taskschd.msc 并按 Enter,打开任务计划程序。
在右侧操作栏中选择“创建任务”。
在“常规”选项卡中,填写任务名称(例如“DisableWindowsUpdate”),并选择“以最高权限运行”。
在“触发器”选项卡中,点击“新建”,选择“在登录时”作为触发器。
在“操作”选项卡中,点击“新建”,选择“启动程序”,然后浏览并选择之前创建的 RunDisableWindowsUpdate.bat 文件。
点击“确定”保存任务。

注意事项

确保PowerShell的执行策略允许运行脚本。你可以在PowerShell中运行以下命令来允许脚本执行:

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

文章目录