net userコマンドでユーザーアカウントとパスワードを作成・削除・管理する


この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので十分ご注意ください。

最近、windowsサーバを構築して、ローカルユーザ設定等の重複作業を担当しましたので、Net userコマンドを整理しておきました。

★前提:管理者権限でコマンドプロンプトを起動する

net userコマンドのヘルプ

net user /?

C:\Users\Administrator>net user /?
The syntax of this command is:

NET USER
[username [password | *] [options]] [/DOMAIN]
         username {password | *} /ADD [options] [/DOMAIN]
         username [/DELETE] [/DOMAIN]
         username [/TIMES:{times | ALL}]
         username [/ACTIVE: {YES | NO}]

アカウント情報の確認

net user

C:\Users\Administrator>net user

User accounts for \\EC2AMAZ-7NGBL89

-------------------------------------------------------------------------------
Administrator            DefaultAccount           Guest
test2admin               testadmin                WDAGUtilityAccount
The command completed successfully.

現在自分がどのアカウントでサインインしているかは、例えば「set user」コマンドで環境変数を表示させると簡単に確認できる。

C:\Users\Administrator>set user
USERDOMAIN=EC2AMAZ-7NGBL89
USERDOMAIN_ROAMINGPROFILE=EC2AMAZ-7NGBL89
USERNAME=Administrator
USERPROFILE=C:\Users\Administrator

アカウントの作成/削除

新しいユーザーアカウントを作成するには、「net user <アカウント名> <パスワード> /add」を実行する。
net user testadmin P@ssw0rd /add

C:\Users\Administrator>net user testadmin P@ssw0rd /add
The command completed successfully.

「/random」を使えば、ランダムなパスワードを生成、「net user <アカウント名> /random /add」を実行する。

C:\Users\Administrator>net user test2admin /random /add
Password for test2admin is: bfU#@GsR

The command completed successfully.

既存のユーザーアカウントを削除するには、「net user <アカウント名> /delete」とする。

C:\Users\Administrator>net user test2admin /delete
The command completed successfully.

ローカルグループの作成/削除

新しいローカルグループを作成するには、「net localgroup グループ名 /ADD [/COMMENT:"テキスト"] 」を実行する。

作成
net localgroup testgroup /add

C:\Users\Administrator>net localgroup testgroup /add
The command completed successfully.

削除
net localgroup testgroup /delete

C:\Users\Administrator>net localgroup testgroup /delete
The command completed successfully.

所属グループの確認/変更

 作成したアカウントは、デフォルトではローカルコンピュータ上の「Users」グループに属する、一般権限のユーザーアカウント(ローカルアカウント)となる。

確認
net localgroup

C:\Users\Administrator>net localgroup

Aliases for \\EC2AMAZ-7NGBL89

-------------------------------------------------------------------------------
*Access Control Assistance Operators
*Administrators
...
*Users
The command completed successfully.

「administrators」グループにtestadminを追加
net localgroup administrators testadmin /add

C:\Users\Administrator>net localgroup administrators testadmin /add
The command completed successfully.`

「Users」グループからtestadminを削除
net localgroup users testadmin /del

C:\Users\Administrator>net localgroup users testadmin /del
The command completed successfully.

パスワードの変更

ユーザーアカウントのパスワードを変更するには、「net user <アカウント名> <パスワード>」を実行する。
net user testadmin 123456a!

C:\Users\Administrator>net user testadmin 123456a!
The command completed successfully.

ところで、このような設定方法ではパスワードがコマンドプロンプトの履歴に残るし、コンソールの背後から他人に見られてしまうかもしれない。そのためには、「<パスワード>」の代わりに「」を指定する。こうすると、パスワードを入力するプロンプトが表示され、2回同じパスワードを入力すると、パスワードが変更される。
net user testadmin

C:\Users\Administrator>net user testadmin *
Type a password for the user:
Retype the password to confirm:
The command completed successfully.

以下、ユーザアカウントに対して、詳細設定

ユーザーは次回ログオン時にパスワードの変更が必要①

不明!
(恐らくコマンドで設定不可能)

ユーザーはパスワードを変更できないの修正②

wmic path Win32_UserAccount where Name=’testadmin’ set PasswordChangeable=false

変更可否チェック
wmic path Win32_UserAccount where Name=’testadmin’ get PasswordChangeable
TRUE の場合、パスワードは変更可能

パスワードを無期限にする③

wmic path Win32_UserAccount where Name=’testadmin’ set PasswordExpires=false(testadminは自身のユーザー名)

C:\Users\Administrator>wmic path Win32_UserAccount where Name='testadmin' set PasswordExpires=false
Updating property(s) of '\\EC2AMAZ-7NGBL89\root\cimv2:Win32_UserAccount.Domain="EC2AMAZ-7NGBL89",Name="testadmin"'
Property(s) update successful.

有効期限チェック
wmic path Win32_UserAccount where Name=’testadmin’ get PasswordExpires
パスワードの有効期限チェックが有効な場合は「TRUE」が返され、無効時は「FALSE」が返されます。

アカウントを無効にする④

wmic path Win32_UserAccount where Name=’testadmin’ set Disabled=true
或いは NET USER testadmin /ACTIVE:No

C:\Users\Administrator>wmic path Win32_UserAccount where Name='testadmin' set Disabled=true
Updating property(s) of '\\EC2AMAZ-7NGBL89\root\cimv2:Win32_UserAccount.Domain="EC2AMAZ-7NGBL89",Name="testadmin"'
Property(s) update successful.

有効/無効チェック
wmic path Win32_UserAccount where Name=’testadmin’ get Disabled
TRUE の場合、ユーザー アカウントは無効です。

アカウントのロックアウト⑤

wmic path Win32_UserAccount where Name=’testadmin’ set Lockout=true

ロックチェック
wmic path Win32_UserAccount where Name=’testadmin’ get Lockout
TRUE の場合、ユーザー アカウントはロックアウトされます。

ローカル ユーザーのフル ネーム

wmic path Win32_UserAccount where Name=’testadmin’ set FullName=testAdminName

lusrmgr.msc でローカルユーザーとグループ(ローカル)画面が開くGUIで確認

file

以上です。

Last modified: 2024-02-01

Author