PowerShell: Create Local User Account

前端 未结 5 2043
情歌与酒
情歌与酒 2020-12-13 03:44

I need to create a new local user account, and then add them to the local Administrators group. Can this be done in PowerShell?

EDIT:



        
相关标签:
5条回答
  • 2020-12-13 04:04

    Try using Carbon's Install-User and Add-GroupMember functions:

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

    Disclaimer: I am the creator/maintainer of the Carbon project.

    0 讨论(0)
  • 2020-12-13 04:13
    Import-Csv C:\test.csv |
    Foreach-Object {
      NET USER    $ _.username   $ _.password /ADD
      NET LOCALGROUP "group" $_.username  /ADD
    }
    

    edit csv as username,password and change "group" for your groupname

    :) worked on 2012 R2

    0 讨论(0)
  • 2020-12-13 04:14

    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.

    0 讨论(0)
  • 2020-12-13 04:23

    Another alternative is the old school NET USER commands:

    NET USER username "password" /ADD

    OK - you can't set all the options but it's a lot less convoluted for simple user creation & easy to script up in Powershell.

    NET LOCALGROUP "group" "user" /add to set group membership.

    0 讨论(0)
  • 2020-12-13 04:27

    As of PowerShell 5.1 there cmdlet New-LocalUser which could create local user account.

    Example of usage:

    Create a user account

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

    or Create a user account that has a password

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

    or Create a user account that is connected to a Microsoft account

    New-LocalUser -Name "MicrosoftAccount\usr name@Outlook.com" -Description "Description of this account." 
    
    0 讨论(0)
提交回复
热议问题