如何使用 PowerShell 创建本地用户帐户
要使用 PowerShell 创建本地用户帐户,请使用 `New-LocalUser` 命令。此命令创建一个本地用户帐户。
这里是创建本地用户帐户的 PowerShell 命令。
New-LocalUser
[-AccountExpires <DateTime>]
[-AccountNeverExpires]
[-Description <String>]
[-Disabled]
[-FullName <String>]
[-Name] <String>
-Password <SecureString>
[-PasswordNeverExpires]
[-UserMayNotChangePassword]
[-WhatIf]
[-Confirm]
[<CommonParameters>]在本文中,我们将讨论如何创建本地用户帐户,创建一个有密码的本地用户帐户。
如何使用 PowerShell 创建本地用户帐户
使用 New-LocalUser 命令创建新的本地用户帐户。
New-LocalUser -Name 'Peter Ambler' -Description 'Description of this account.' -NoPassword在上面的 PowerShell 脚本中,New-LocalUser 创建一个新的本地用户帐户“Peter Ambler”。它不指定 AccountExpires 或 Password 参数。本地用户帐户不会过期或有密码。
下面给出了上述用于创建本地用户帐户的 PowerShell 脚本的输出。
如何创建具有密码的新本地用户帐户
要创建具有密码的新本地用户帐户,请使用带有密码参数的 New-LocalUser cmdlet。
$Password = Read-Host -AsSecureString
$params = @{
Name = 'James Allen'
Password = $Password
Description = 'Description of this account.'
}
New-LocalUser @params在上面的 PowerShell 脚本中,首先,Read-Host cmdlet 提示您输入密码并将其作为安全字符串存储在 $Password 变量中。 $params 变量存储本地用户帐户详细信息,例如名称、密码和描述。
最后,New-LocalUser 命令使用 $params 创建具有密码的本地用户帐户。
下面给出了创建带有密码的本地用户帐户的上述 PowerShell 的输出。
$Password = Read-Host -AsSecureString
$params = @{
Name = 'James Allen'
Password = $Password
Description = 'Description of this account.'
}
New-LocalUser @params
Name Enabled Description
---- ------- -----------
James Allen True Description of this account.
PS C:\>结论
希望上述有关如何使用 New-LocalUser PowerShell 命令创建新的本地用户帐户的文章对您有所帮助。
您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。
相关链接
Microsoft.PowerShell.LocalAccounts 模块
获取本地组成员
获取本地用户
添加本地组成员
启用本地用户
禁用本地用户
重命名-本地用户
设置本地用户
