Post

Creating user accounts from CSV in PowerShell
This PowerShell scripts shows how to create user accounts that are located in a .csv file and move it to a specified OU. You can add lots of attributes to the New-ADUser cmdlet for example: –mail –manager –mobile etc. I created a users.csv file before I executed this script: FirstName LastName SamAccountName Password Maarten Peeters m.peeters […]

This PowerShell scripts shows how to create user accounts that are located in a .csv file and move it to a specified OU.
You can add lots of attributes to the New-ADUser cmdlet for example: –mail –manager –mobile etc.

I created a users.csv file before I executed this script:

FirstName LastName SamAccountName Password
Maarten Peeters m.peeters P@ssw0rd!

 

# Created by Maarten Peeters
# https://www.sharepointfire.com
# Created this script using Windows Server 2012# Import Active Directory module
Import-Module ActiveDirectory -ErrorAction SilentlyContinue# Set OU for the user accounts
$OU = “OU=Users,OU=Domain environment,DC=peet,DC=local”

# Get domain name
$dnsroot = ‘@’ + (Get-ADDomain).dnsroot

# Import the file with the users.
$users = Import-Csv .\users.csv -Delimiter “;”

foreach ($user in $users)
{

# Get password from CSV File
$Password = (ConvertTo-SecureString “$($user.Password)” -AsPlainText -force)

# Create Accounts

$FullName = ($user.FirstName + ” ” + $user.LastName)
$UserPrincipalName = ($user.SamAccountName + $dnsroot)

try
{
New-ADUser -SamAccountName $user.SamAccountName –Name $FullName –DisplayName $FullName -GivenName $user.FirstName -Surname $user.LastName –UserPrincipalName $UserPrincipalName -Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires $false -Path $OU -AccountPassword $Password -PassThru | Out-Null
Write-Output “Created user $($user.SamAccountName)”
}
catch [System.Object]
{
Write-Output “Could not create user $($user.SamAccountName), $_”
}

}

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Archive