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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/162304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:28:09  来源:igfitidea点击:

How do I shutdown, restart, or log off Windows via a bat file?

windowsbatch-filecommand-linerestartshutdown

提问by Keng

I've been using Remote Desktop Connectionto 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?

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

回答by Keng

The most common ways to use the shutdowncommand are:

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

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

    Note:There is a common pitfall wherein users think -hmeans "help" (which it does for every other command-line program... except shutdown.exe, where it means "hibernate"). They then run shutdown -hand 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.
  • shutdown -s- 关闭。
  • shutdown -r— 重新启动。
  • shutdown -l— 注销。
  • shutdown -h— 休眠。

    注意:有一个常见的陷阱,用户认为它的-h意思是“帮助”(它对其他所有命令行程序都是如此……除了shutdown.exe,它的意思是“休眠”)。然后他们跑了shutdown -h,不小心关掉了他们的电脑。请注意这一点。

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

  • shutdown -a— 中止先前的关机命令。

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 0to 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.

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

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



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

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

回答by JosephStyons

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

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

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.

正如您在评论中看到的,-f 是由超时隐含的。

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

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

回答by Kip

No one has mentioned -moption for remote shutdown:

没有人提到-m远程关机的选项:

shutdown -r -f -m \machinename

Also:

还:

  • The -rparameter causes a reboot (which is usually what you want on a remote machine, since physically starting it might be difficult).
  • The -fparameter option forces the reboot.
  • You must have appropriate privileges to shut down the remote machine, of course.
  • -r参数会导致重新启动(这通常是您在远程机器上想要的,因为物理启动它可能很困难)。
  • -f参数选项强制重启。
  • 当然,您必须具有适当的权限才能关闭远程机器。

回答by VonC

Original answer: Oct. 2008

原答案:2008 年 10 月

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

你也得到了所有 rundll32.exe shell32.dll系列:

(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
    
  • rundll32.exe user.exe,**ExitWindows**【快速关闭Windows】
  • rundll32.exe user.exe,**ExitWindowsExec**[重新启动 Windows]

    rundll32.exe shell32.dll,SHExitWindowsEx n
    

where nstands for:

其中n代表:

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

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

(可以组合 -> 6 = 2+4 FORCE REBOOT



Update April 2015 (6+ years later):

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

1800 INFORMATIONkindly points out in the comments:

1800 INFORMATION在评论中指出

Don't use rundll32.exefor 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.

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

Raymond CHENwrote:

Raymond Chen写道:

The function signature required for functions called by rundll32.exeis:

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

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

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

这并没有阻止人们使用rundll32调用随机函数,这些函数不是设计为由rundll32、 像user32 LockWorkStation或调用的user32 ExitWindowsEx

(oops)

(哎呀)

The actual function signature for ExitWindowsEx is:

ExitWindowsEx 的实际函数签名是:

BOOL WINAPI ExitWindowsEx(UINT uFlags, DWORD dwReserved);

And to make it crystal-clear:

并使其清晰:

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

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

回答by Gavin

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.

此外,由于它是计时的,您会获得与在俄勒冈小径打猎时相同的刺激。

回答by Carl

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

当远程连接到一台机器时(目标是 Windows XP;我不确定目标是 Windows Vista),虽然开始菜单上的关机断开连接会话或类似的东西取代,但应该有一个叫做“Windows 安全”的也可以同样的事情Ctrl+ Alt+End尖的由欧文。

回答by Dean Rather

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.

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

Saves leaving batch files lying around everywhere.

保存到处都是批处理文件。

回答by Dean Rather

I would write this in Notepador WordPadfor 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.

这与手动单击开始和注销基本相同,但如果您准备好批处理文件,速度会稍微快一些。

回答by msoft

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.

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

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

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

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

有关屏幕截图甚至视频,请参阅:https: //tinkertry.com/how-to-shutdown-or-restart-windows-over-rdp