SID-500

Home » PowerShell » PowerShell: Adding Active Directory Users from Text Files (Bulk)

PowerShell: Adding Active Directory Users from Text Files (Bulk)

When it comes to importing huge numbers of users, csv is No. 1. But what about text files? Sure, you can export content of text files to csv.  But in this article, I want to add active directory users directly from text files.

Introduction

For the following I’m using this text file. Similarities with persons in the movie “Pumping Iron” are purely accidential 😉

Unbenannt.JPG

I’m sure most administrators are not happy with it. And most administrators will start converting to csv. But I am going to take it as it is.

1. Using Split to add users

Which brings me to split. What is split? Split is a PowerShell method. Split can help you split something. And we have to split our list. We need a list of given names and surnames. So, we have to tell PowerShell, where to split. We want to split at the blank position.

Example:

Unbenannt.JPG

("Patrick Grünauer").Split(" ")

Unbenannt.JPG

Now we call them separately.

$name=("Patrick Grünauer").Split(" ")
$name[0]
$name[1]

Unbenannt.JPG

Cool Stuff. That’s exactly what we need for the next step.

My file consists of first names and surnames. It’s the same file as above.

Unbenannt

The following code

  • splits the list into given name and surname
  • adds new Active Directory users with a UserPrincipalName (surname), SamAccountName (surname), Full Name (first name + surname) and AccountPassword
  • enables each user
  • sets ChangePasswortAtLogon to True
  • shows what happens (verbose parameter)

Here we go:

Get-Content C:\temp\users.txt | ForEach-Object {$Split = $_.Split(" "); $given=$Split[0]; $sur=$Split[1]; New-ADUser -GivenName $given -Surname $sur -Name ($given + " " + $sur) -UserPrincipalName (($sur + "@" + "$env:userdnsdomain")).ToLower() -SamAccountName ($sur).ToLower() -AccountPassword (ConvertTo-SecureString -AsPlainText "Passw00rd123" -Force) -Enabled $true -ChangePasswordAtLogon $true -Verbose}

Unbenannt.PNG

Unbenannt.JPG

Split was sucessful. Pay attention to First name and Last name.

Unbenannt.JPG

All users are enabled and the login names (UPN, SamAccountname) are correct.

Unbenannt.PNG

2. Using ConvertFrom-String (PowerShell 5.0 and above)

Now let’s turn to the simpler solution. If you are sitting in front of a server running PowerShell 5.0 and above, you can use ConvertFrom-String. See what ConverFrom-String can do:

Get-Content C:\temp\Users.txt | ConvertFrom-String

Unbenannt.PNG

So we have P1 and P2. That’s great.

Get-Content C:\temp\users.txt | ConvertFrom-String | ForEach-Object {New-ADUser -GivenName $_.P1 -Surname $_.P2 -Name ($_.P1 + " " + $_.P2) -UserPrincipalName (($_.P2 + "@" + "$env:userdnsdomain")).ToLower() -SamAccountName ($_.P2).ToLower() -AccountPassword (ConvertTo-SecureString -AsPlainText "Passw000rd123" -Force) -Enabled $true -ChangePasswordAtLogon $true -Verbose}

Unbenannt.PNG

Great stuff. Hope you like it too.

Have fun playing with text files!

See also

For more about bulk operations see my articles

PowerShell: Enable Remote Desktop on multiple Servers remotely (Bulk)

PowerShell: Changing Active Directory user logon names (Bulk)

*** German *** Nano Server (Teil 8): Mehrere Nano Server auf einmal installieren (Bulk)


3 Comments

  1. Anirban says:

    How can we move users from one forest into another forest in windows server 2012 r2

    Like

  2. […] PowerShell: Adding Active Directory Users from Text Files (Bulk) […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

WHOIS

My name is Patrick Grünauer (pewa2303). I am from Austria. On sid-500 I write about Windows, Cisco and IT-Security in English and German. Have fun while reading!

Patrick Gruenauer
Follow SID-500 on WordPress.com