如何通过 bat 文件关闭、重新启动或注销 Windows?

发布于 2024-07-06 09:20:59 字数 215 浏览 12 评论 0 原文

我一直在使用远程桌面连接进入工作站。 但在这种环境下,我无法使用“开始”菜单中的电源选项。 我需要一种替代方法来关闭或重新启动。

如何通过命令行控制计算机的电源状态?

I've been using Remote Desktop Connection to get into a workstation. But in this environment, I cannot use the power options in Start Menu. I need an alternative way to shutdown or restart.

How do I control my computer's power state through the command line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

分分钟 2024-07-13 09:20:59

使用 shutdown 命令的最常见方法是:

  • shutdown -s — 关闭。
  • shutdown -r — 重新启动。
  • shutdown -l — 注销。
  • shutdown -h — 休眠。

    注意:有一个常见的陷阱,即用户认为 -h 表示“帮助”(它对所有其他命令行程序都是如此......除了 >shutdown.exe,它的意思是“休眠”)。 然后,他们运行 shutdown -h 并意外关闭了计算机。 请注意这一点。

  • shutdown -i —“交互模式”。 它不执行操作,而是显示 GUI 对话框。

  • shutdown -a — 中止之前的关闭命令。

上面的命令可以与以下附加选项结合使用:

  • -f — 强制程序退出。 防止关闭过程卡住。
  • -t — 设置关机前的时间。 使用-t 0立即关闭。
  • -c — 添加关闭消息。 该消息最终将出现在事件日志中。
  • -y — 强制对所有关闭查询回答“是”。

    注意:此选项未记录在任何官方文档中。 它是由这些 StackOverflow 用户发现的。


我想确保除了这个答案之外还提到了其他一些非常好的答案。 在这里,它们没有特定的顺序。

The most common ways to use the shutdown command are:

  • shutdown -s — Shuts down.
  • shutdown -r — Restarts.
  • shutdown -l — Logs off.
  • shutdown -h — Hibernates.

    Note: There is a common pitfall wherein users think -h means "help" (which it does for every other command-line program... except shutdown.exe, where it means "hibernate"). They then run shutdown -h and accidentally turn off their computers. Watch out for that.

  • shutdown -i — "Interactive mode". Instead of performing an action, it displays a GUI dialog.

  • shutdown -a — Aborts a previous shutdown command.

The commands above can be combined with these additional options:

  • -f — Forces programs to exit. Prevents the shutdown process from getting stuck.
  • -t <seconds> — Sets the time until shutdown. Use -t 0 to shutdown immediately.
  • -c <message> — Adds a shutdown message. The message will end up in the Event Log.
  • -y — Forces a "yes" answer to all shutdown queries.

    Note: This option is not documented in any official documentation. It was discovered by these StackOverflow users.


I want to make sure some other really good answers are also mentioned along with this one. Here they are in no particular order.

狠疯拽 2024-07-13 09:20:59

如果您在远程计算机上,您可能还需要添加 -f 选项来强制重新启动。 否则,您的会话可能会关闭,并且顽固的应用程序可能会挂起系统。

每当我想强制立即重新启动时,我都会使用此选项:

shutdown -t 0 -r -f

对于更友好的“给他们一些时间”选项,您可以使用此选项:

shutdown -t 30 -r

正如您在注释中看到的, -f 是由超时暗示的。

Brutus 2006 是一个为这些选项提供 GUI 的实用程序。

If you are on a remote machine, you may also want to add the -f option to force the reboot. Otherwise your session may close and a stubborn app can hang the system.

I use this whenever I want to force an immediate reboot:

shutdown -t 0 -r -f

For a more friendly "give them some time" option, you can use this:

shutdown -t 30 -r

As you can see in the comments, the -f is implied by the timeout.

Brutus 2006 is a utility that provides a GUI for these options.

雪花飘飘的天空 2024-07-13 09:20:59

没有人提到用于远程关闭的 -m 选项:

shutdown -r -f -m \\machinename

另外:

  • -r 参数会导致重新启动(这通常是您在远程计算机上想要的,因为物理启动它)可能很难)。
  • -f 参数选项强制重新启动。
  • 当然,您必须具有适当的权限才能关闭远程计算机。

No one has mentioned -m option for remote shutdown:

shutdown -r -f -m \\machinename

Also:

  • The -r parameter causes a reboot (which is usually what you want on a remote machine, since physically starting it might be difficult).
  • The -f parameter option forces the reboot.
  • You must have appropriate privileges to shut down the remote machine, of course.
无人接听 2024-07-13 09:20:59

原始答案:2008 年 10 月

您还获得了所有 "rundll32.exe shell32.dll" 系列:(

请参阅下面的更新)

  • rundll32.exe user.exe,**ExitWindows** [快速关闭 Windows]
  • rundll32.exe user.exe,**ExitWindowsExec** [重新启动 Windows]

    rundll32.exe shell32.dll,SHExitWindowsEx n 
      

其中 n 代表:

  • 0 - LOGOFF< /code>
  • 1 - SHUTDOWN
  • 2 - REBOOT
  • 4 - FORCE
  • 8 - POWEROFF

(可以组合 -> 6 = 2+4 强制重启


2015 年 4 月更新(6 年多后):

1800 信息< /a> 请指出评论

请勿将 rundll32.exe 用于此目的。 它期望您在命令行上传递的函数具有非常具体的方法签名 - 它与 ExitWindows 的方法签名不匹配。

Raymond CHEN 写道:

rundll32.exe调用的函数所需的函数签名是:

void CALLBACK ExitWindowsEx(HWND hwnd, HINSTANCE hinst,
       LPSTR pszCmdLine, int nCmdShow);

这并没有阻止人们使用 rundll32 调用并非由 rundll32 调用的随机函数,例如 user32 LockWorkStation > 或 user32 ExitWindowsEx

(哎呀)

ExitWindowsEx 的实际函数签名是:

BOOL WINAPI ExitWindowsEx(UINT uFlags, DWORD dwReserved);

并使其清晰可见:

Rundll32 是 Windows 95 的遗留物,至少从 Windows Vista 开始就已被弃用,因为它违反了许多现代工程准则。

Original answer: Oct. 2008

You also got all the "rundll32.exe shell32.dll" serie:

(see update below)

  • rundll32.exe user.exe,**ExitWindows** [Fast Shutdown of Windows]
  • rundll32.exe user.exe,**ExitWindowsExec** [Restart Windows]

    rundll32.exe shell32.dll,SHExitWindowsEx n
    

where n stands for:

  • 0 - LOGOFF
  • 1 - SHUTDOWN
  • 2 - REBOOT
  • 4 - FORCE
  • 8 - POWEROFF

(can be combined -> 6 = 2+4 FORCE REBOOT)


Update April 2015 (6+ years later):

1800 INFORMATION kindly points out in the comments:

Don't use rundll32.exe for this purpose. It expects that the function you passed on the command line has a very specific method signature - it doesn't match the method signature of ExitWindows.

Raymond CHEN wrote:

The function signature required for functions called by rundll32.exe is:

void CALLBACK ExitWindowsEx(HWND hwnd, HINSTANCE hinst,
       LPSTR pszCmdLine, int nCmdShow);

That hasn't stopped people from using rundll32 to call random functions that weren't designed to be called by rundll32, like user32 LockWorkStation or user32 ExitWindowsEx.

(oops)

The actual function signature for ExitWindowsEx is:

BOOL WINAPI ExitWindowsEx(UINT uFlags, DWORD dwReserved);

And to make it crystal-clear:

Rundll32 is a leftover from Windows 95, and it has been deprecated since at least Windows Vista because it violates a lot of modern engineering guidelines.

孤独患者 2024-07-13 09:20:59

另一个小提示:当使用批处理文件路径时,我希望能够中止它,以防我意外运行它。 因此,批处理文件会调用关闭,但随后会将您留在命令提示符处。

@echo off
echo Shutting down in 10 seconds. Please type "shutdown /a" to abort.
cmd.exe /K shutdown /f /t 10 /r

另外,由于它是有计时器的,所以你会得到与狩猎时相同的刺激感在俄勒冈小道

Another small tip: when going the batch file route, I like to be able to abort it in case I run it accidentally. So the batch file invokes the shutdown but leaves you at the command prompt afterwards.

@echo off
echo Shutting down in 10 seconds. Please type "shutdown /a" to abort.
cmd.exe /K shutdown /f /t 10 /r

Plus, since it's on a timer, you get about the same thrill as you do when hunting in The Oregon Trail.

薆情海 2024-07-13 09:20:59

您可能已经意识到这一点,但以防万一:只需在“运行”框中输入 shutdown -r(或任何您喜欢的命令)并按 Enter 键会更容易。

避免将批处理文件散落在各处。

You're probably aware of this, but just in case: it's much easier to just type shutdown -r (or whatever command you like) into the "Run" box and hit enter.

Saves leaving batch files lying around everywhere.

森罗 2024-07-13 09:20:59

当远程连接到计算机时(无论如何,目标是 Windows XP;我不确定目标 Windows Vista),尽管开始菜单上的“关闭”被替换为“断开会话”或类似的东西,应该有一个名为“Windows Security”的东西,它也可以做与 Ctrl + Alt + End 相同的事情,如所指出的欧文.

When remoted into a machine (target is Windows XP anyway; I am not sure about target Windows Vista), although Shutdown on the start menu is replaced by Disconnect Session or something like that, there should be one called 'Windows Security' which also does the same thing as Ctrl + Alt + End as pointed to by Owen.

烈酒灼喉 2024-07-13 09:20:59

我参加聚会迟到了,但还没有看到这个答案。
当您不想使用批处理文件或键入命令时。 您可以将焦点设置到桌面,然后使用 Alt + F4

Windows 会询问你要做什么,选择关机或重新启动。

有关屏幕截图甚至视频,请参阅:https://tinkertry。 com/如何关闭或重新启动 Windows-over-rdp

I'm late to the party, but did not see this answer yet.
When you don't want to use a batch file or type the command. You can just set focus to the desktop and then use Alt + F4.

Windows will ask you what you want to do, select shutdown or restart.

For screenshots and even a video, see: https://tinkertry.com/how-to-shutdown-or-restart-windows-over-rdp

靑春怀旧 2024-07-13 09:20:59

我会在 记事本写字板 用于基本注销命令:

@echo off
shutdown -l

这与手动单击开始和注销基本相同,但如果您有批处理文件准备好了。

I would write this in Notepad or WordPad for a basic logoff command:

@echo off
shutdown -l

This is basically the same as clicking start and logoff manually, but it is just slightly faster if you have the batch file ready.

眼眸里的快感 2024-07-13 09:20:59

shutdownrundll32.exe shell32.dll,SHExitWindowsEx n命令。

注销 - 允许您通过 sessionid 或会话名称注销用户

PSShutdown - 需要从 Windows sysinternals 下载。

bootim.exe - Windows 10/8 关闭 iu

change/chglogon - 阻止新用户登录或进行另一个会话

NET SESSION /DELETE - 结束用户

wusa /forcerestart /quiet - Windows 更新管理器,还可以重新启动机器

tsdiscon - 断开您

rdpinit 的连接 -将您注销,尽管我目前找不到任何文档

Some additions to the shutdown and rundll32.exe shell32.dll,SHExitWindowsEx n commands.

LOGOFF - allows you to logoff user by sessionid or session name

PSShutdown - requires a download from windows sysinternals.

bootim.exe - windows 10/8 shutdown iu

change/chglogon - prevents new users to login or take another session

NET SESSION /DELETE - ends a session for user

wusa /forcerestart /quiet - windows update manager but also can restart the machine

tsdiscon - disconnects you

rdpinit - logs you out , though I cant find any documentation at the moment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文