Wednesday 4 February 2015

Creating multiple local users accounts from a csv file using PowerShell then add to the Administrators group

# This script will create multiple local user accounts using the content of a 
# csv file and then add them to the local Administrators group.

<# example CSV layout to be saved as C:\Temp\Users.csv
UserName,FullName,Description
JBloggs,Joe Bloggs,Blogger
JDoe,Jane Doe, Dough Maker

#>

$strComputer=$env:computername

# Import CSV to $AllUsers
$AllUsers = Import-CSV "C:\Temp\Users.csv"

# Set temp password
$TempPassword = "P@ssword1"

foreach ($User in $AllUsers)
      {
      write-host Creating user account $user.Username

      $objOU = [adsi]"WinNT://."

      # Create user account
      $objUser = $objOU.Create("User", $User.Username)

      # Set password
      $objuser.setPassword($TempPassword)

      # Set FullName
      $objUser.FullName = $User.FullName

      # Set Description
      $objUser.Description = $User.Description
     
      # User must change password on next log on
      $objuser.PasswordExpired = 1

      # Save the info
      $objuser.SetInfo()

      # Add each user account to local Administrators group
      $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
      $group = $computer.psbase.children.find("Administrators"
      $group.Add("WinNT://" + $strComputer + "/" + $user.Username

      }

2 comments:

  1. Fix Full name and Description add password
    User cannot change password and never expire

    # This script will create multiple local user accounts using the content of a
    # csv file and then add them to the local Administrators group.

    <# example CSV layout to be saved as C:\Temp\Users.csv
    UserName,FullName,Description
    JBloggs,Joe Bloggs,Blogger
    JDoe,Jane Doe, Dough Maker

    #>

    $strComputer=$env:computername

    # Import CSV to $AllUsers
    $AllUsers = Import-CSV "C:\Users.csv"

    foreach ($User in $AllUsers)
    {
    write-host Creating user account $user.Username

    $objOU = [adsi]"WinNT://."

    # Create user account
    $objUser = $objOU.Create("User", $User.Username)

    # Set password
    $objuser.setPassword($User.Password)

    # Set FullName
    $objUser.put("FullName",$User.FullName)

    # Set Description
    $objUser.put("Description",$User.Description)

    # User must change password on next log on
    #$objuser.PasswordExpired = 0

    # User cannot change password and never expire
    $objUser.UserFlags = 64 + 65536



    # Save the info
    $objuser.SetInfo()

    # Add each user account to local Administrators group
    $computer = [ADSI]("WinNT://" + $strComputer + ",computer")
    $group = $computer.psbase.children.find("STB")
    $group.Add("WinNT://" + $strComputer + "/" + $user.Username)

    }

    ReplyDelete