如何编写每隔60秒移动鼠标的PowerShell脚本?
PowerShell脚本:每隔60秒自动小幅移动鼠标
以下脚本通过System.Windows.Forms命名空间模拟鼠标小幅移动,每隔60秒执行一次,可用于避免系统自动休眠或锁定:
# 加载System.Windows.Forms程序集 Add-Type -AssemblyName System.Windows.Forms Write-Host "脚本已启动,每隔60秒移动一次鼠标。按Ctrl+C终止脚本。" # 无限循环执行鼠标移动操作 while ($true) { # 获取当前鼠标位置 $currentPos = [System.Windows.Forms.Cursor]::Position # 小幅移动鼠标(向右下偏移1像素) [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($currentPos.X + 1, $currentPos.Y + 1) # 移回原位置,避免鼠标位置持续偏移 Start-Sleep -Milliseconds 10 [System.Windows.Forms.Cursor]::Position = $currentPos # 等待60秒后再次执行 Start-Sleep -Seconds 60 }
关键细节说明:
- 程序集加载:
Add-Type -AssemblyName System.Windows.Forms是调用Windows窗体鼠标API的必要步骤 - 移动逻辑:每次仅做1像素的微小偏移后立即归位,既触发系统的"用户活动"检测,又不会干扰正常鼠标操作
- 终止方式:在PowerShell窗口按下
Ctrl+C即可停止脚本运行
内容的提问来源于stack exchange,提问作者swifty checkout
相关产品推荐
相关产品推荐

