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

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?

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.
The -f option from JosephStyons
Using rundll32 from VonC
The Run box from Dean
Remote shutdown from Kip

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.

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.

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:
in 2004 "What can go wrong when you mismatch the calling convention?":
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);
in 2011: "Throwing garbage on the sidewalk: The sad history of the rundll32 program"
And to make it crystal-clear:
in 2013 "What's the guidance on when to use rundll32? Easy: Don't use it":
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.

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.

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.

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.

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

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.

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

Related

Using TortoisePLink in GIT_SSH_COMMAND: "-batch" switch not respected

(OS: Windows; using PowerShell)
Background
I intend to have "git pull" run from time to time by my service (i.e. Windows Service). In order to achieve it I use TortoiseGitPLink to provide git with my SSH key every time it's needed. This is done by setting the env var:
$env:GIT_SSH_COMMAND="'C:/Program Files/TortoiseGit/bin/TortoisePlink.exe' -i c:/path/to/my/key/id_rsa.ppk -pw myKeyPassword"
This works fine in general, but if I make a mistake and specify wrong path to my SSH key or wrong password, then TortoisePLink hangs indefinitely, freezing therefore my service. Also, If I stop the service, there's still TortoisePLink process hanging around.
Reading Plink's doc I found a "-batch" switch:
-batch: disable all interactive prompts
but for some reason either it is not respected or I use it in a wrong way.
Question
Is there a way to use TortoiseGitPLink to reliably provide an SSH key? By "reliably" I mean: it should fail if any pop-up appears or the key file is missing.

Is there a way to get my laptop to beep from within a bash script running on a remote server via SSH?

I have a bash script that I have to regularly run on a remote server. Part of the script includes running a backup which takes a while, and after it has run, I have to hit "Y" to confirm that the backup worked before the script will continue.
I would like to know if there is a way to get my laptop to make a beep (or some sort of sound) when that happens. I know that echo -e '\a' makes a beep, but if I run it from within a script on the remote server, the beep happens on the remote server.
I have control of the script that is being run, so I could easily change it to do something special.
You could send the command through ssh back to your computer like:
ssh user#host "echo -e '\a'"
Just make sure you have ssh key authentication from your server to your computer so the command can run smoothly
In my case the offered solutions with echo didn't work. I'm using a macbook and connect to an ubuntu system. I keep the terminal open and I'd like to be informed when a long running bash script is ready.
What I did notice is that if I shutdown the remote system then it will beep the macbook and show an alarm icon on the relevant tab. So I have now implemented a bit of dirty workaround:
sudo shutdown 1440 && shutdown -c
This will initiate the system to shutdown and will immediately cancel the request. And I do get the alarm beep + icon. You will need to setup sudo to allow the user to permit shutdown. As it was my own remote server it was no problem but could limit the usability for others.

How to properly start a Gnome-Shell extension via command line?

After copying myexentension#me.com folder to .../gnome-shell/extensions/ I'm executing this command on the terminal:
gnome-shell-extension-tool -e myexentension#me.com
Then, I restart my session with Alt + F2 and execute r, and everything works fine.
But can I start my extension only through the command line? Without Alt+F2+r? Without restarting my gnome-shell session?
According to some answers around the internet, sending SIGHUP to the gnome-shell process restarts it (i. e. killall -HUP gnome-shell), but I haven’t been able to find a clear source on this and couldn’t find the signal handling in the code. What I do know is that this should be exactly equivalent to Alt+F2 r:
busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s 'Meta.restart("Restarting…")'
Because apart from a gettext call on the message, this is exactly what Alt+F2 r is bound to (see runDialog.js – search for _restart).
January 2022 update: Since Gnome 41, calling Eval is restricted and requires “unsafe mode” to be enabled, so by default this will no longer work. I’m not currently aware of a replacement for this particular usage.
Personally, I prefer a solid Alt+F2, r+Enter but maybe try disabling and enabling:
gnome-shell-extension-tool -d myexentension#me.com && gnome-shell-extension-tool -e myexentension#me.com
or
gnome-shell-extension-tool -r myexentension#me.com
Which may do the same thing. There's also gnome-shell-extension-prefs which you can use to do the same thing (and is typically hidden in Gnome for some reason).

PSExec does not show output of the started child process (local machine)

We are in the need of spawning processes from session 0 isolation to a logged in user in session 1.
This task is done by using psexec, this happens entirely on the same machine, but I am unable to get the output of the child process to be displayed in psexec. Since this runs on a build-server I don't want to redirect output to a file, etc
Here is a sample call which does not work:
C:\>PsExec.exe cmd.exe -i 1 -u user -p pwd /C echo foo
The result shows nowhere any "foo"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
cmd.exe exited with error code 0.
One thing that works, but is currently useless is to ommit -i and add \\localhost:
C:\>PsExec.exe \\localhost cmd.exe /C echo foo
returns:
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
foo
cmd.exe exited on localhost with error code 0.
But when I specify session 1, it breaks. This:
C:\>PsExec.exe cmd.exe -i 1 -u user -p pwd \\localhost /C echo foo
again shows no foo for me.
Again: The intention is not to tee or redirect echo foo into some file and then perform type myfile.log as a second step but to receive the output in the original psexec.
We are bound to session 1 as we need a Direct 3D context, having the service configured with "Allow interacting with the desktop" does not help here.
I don't think that what you are asking is possible. psexec -i runs the process interactively on the remote machine, so everything including the console I/O happens in the respective session on the remote machine.
The -i behavior does in fact make sense. Suppose for example that you ran a batch file which required user input e.g. had a pause or set /p. With -i that input would need to be entered on the target machine, not on the machine where you run psexec. Similarly, the output goes (only) to the remote machine.
The other part of your question is the difference between psexec \\localhost and psexec without a computer name at all. This one appears to be an undocumented psexec quirk, which has been noticed and reported before, for example (with a bit of reading between the lines) at Redirect output of process started locally with PSExec.

Regarding -i option usage of psexec

I was successfully running psexec to open application on remote PC using the following command:
psexec -s -i 1 \\135.20.230.160 -u administrator -p force calc
But suddenly today I found that 'calc' is not opening in the remote machine. Instead it is just running on the process list in task manager.
After some experiments when I changed '-i 1' to '-i 2' I found it working again.
Can anyone explain why this happened and how can I decide that the session number needs to be changed?
I need to build automation script for different users, so this is important to resolve.
Thanks.
You can use tasklist to display all tasks and see what session they are currently running on under the session# section.
The Psexec -i is asking for the session you would like to use.
Therefore as users log in to the machine the session numbers can be anywhere from 0 and up. To find out use tasklist with and check a process you know is running and view its session number.

Resources