LoginSignup
2
3

More than 3 years have passed since last update.

Windowsのコマンドでシャットダウン、再起動、スリープ、休止状態、ログオフ、ロック、ユーザーの切り替えをする方法

Last updated at Posted at 2021-06-23

コマンドでWindowsをシャットダウン、再起動、スリープ、休止状態、ログオフ、ロック、ユーザーの切り替えをする方法をまとめてみました。

シャットダウン

shutdown /s /t 0

再起動

shutdown /r /t 0

スリープ

(psshutdown.exeのパス) /d /t 0 /accepteula

psshutdown.exeはここからダウンロードできます。
https://docs.microsoft.com/ja-jp/sysinternals/downloads/psshutdown

参考:Windows 10 シャットダウンや再起動などのショートカット

休止状態

shutdown /h

ログオフ

shutdown /l

ロック

rundll32 user32.dll,LockWorkStation

ユーザーの切り替え

(SwitchUser.exeのパス)

SwitchUser.exeはここからダウンロードできます。
Release SwitchUser · toracatman/SwitchUser
32ビットのWindowsではSwitchUser.zip、64ビットのWindowsではSwitchUserX64.zipをダウンロードしてください。

SwitchUser.exeが実行できない場合はこちらをインストールしてください。
Download Visual Studio 2015 の Visual C++ 再頒布可能パッケージ from Official Microsoft Download Center

SwitchUser.exeのソースコード

SwitchUser.c
#include <Windows.h>
#include <WtsApi32.h>

#pragma comment(lib, "wtsapi32.lib")

int WINAPI WinMain(HINSTANCE hI, HINSTANCE hP, LPSTR lpC, int nC)
{
    HKEY hKey;
    DWORD dwDisposition;
    int data = 1;
    RegCreateKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI\\UserSwitch", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, &dwDisposition);
    RegSetValueEx(hKey, L"Enabled", 0, REG_DWORD, (const BYTE*)&data, (DWORD)sizeof(data));
    RegCloseKey(hKey);
    WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, FALSE);

    return 0;
}

簡単に説明すると、レジストリの
HKEY_LOCAL_MACHINE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\UserSwitch
Enabledという値を作って1にし、WTSDisconnectSession関数を呼べばユーザーの切り替えになるそうです。

このソースコードはClassic Shellのソースコードの一部(ユーザーの切り替えの部分)をお借りしました。
GitHub - coddec/Classic-Shell: Original code of Classic Shell (v4.3.1), original author Ivo Beltchev

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3