PowerShell的:创建本地用户帐户(PowerShell: Create Local User

2019-07-20 19:52发布

我需要创建一个新的本地用户帐户,然后将它们添加到本地管理员组。 这可以在PowerShell中做什么?

编辑:

# Create new local Admin user for script purposes
$Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"

$LocalAdmin = $Computer.Create("User", "LocalAdmin")
$LocalAdmin.SetPassword("Password01")
$LocalAdmin.SetInfo()
$LocalAdmin.FullName = "Local Admin by Powershell"
$LocalAdmin.SetInfo()
$LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
$LocalAdmin.SetInfo()

我有这个,但不知道是否有什么更PowerShell的去年秋季。

Answer 1:

另一种选择是老同学NET USER命令:

NET USER username "password" /ADD

OK - 你不能将所有的选项,但它是简单的用户创建和易于少了很多令人费解的脚本了在PowerShell中。

NET LOCALGROUP "group" "user" /add设置组成员。



Answer 2:

由于PowerShell的5.1有cmdlet的New-LocalUser它可以创建本地用户帐户。

使用示例:

创建用户帐户

New-LocalUser -Name "User02" -Description "Description of this account." -NoPassword

或者创建具有密码的用户帐户

$Password = Read-Host -AsSecureString
New-LocalUser "User03" -Password $Password -FullName "Third User" -Description "Description of this account."

或创建一个连接到Microsoft帐户的用户帐户

New-LocalUser -Name "MicrosoftAccount\usr name@Outlook.com" -Description "Description of this account." 


Answer 3:

尝试使用Carbon公司的 安装,用户和附加GroupMember功能:

Install-User -Username "User" -Description "LocalAdmin" -FullName "Local Admin by Powershell" -Password "Password01"
Add-GroupMember -Name 'Administrators' -Member 'User'

免责声明:我是碳项目的创建者/维护者。



Answer 4:

As of 2014, here is a statement from a Microsoft representative (the Scripting Guy):

As much as we might hate to admit it, there are still no Windows PowerShell cmdlets from Microsoft that permit creating local user accounts or local user groups. We finally have a Desired State Configuration (DSC ) provider that can do this—but to date, no cmdlets.



Answer 5:

Import-Csv C:\test.csv |
Foreach-Object {
  NET USER    $ _.username   $ _.password /ADD
  NET LOCALGROUP "group" $_.username  /ADD
}

编辑CSV如用户名,密码和更改“组”为您的组名

:)工作在2012 R2



文章来源: PowerShell: Create Local User Account