PowerShell

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 ConvertFrom-String can do for you:

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)

6 replies »

  1. I ran into an issue with creating new user accounts with a script. What I found is that the sAMAccountName has a length limit of 20 characters. This is hard coded and cannot be changed. The case that I had was combining the givenname and surname with some other text. This combined pushed the attempted sAMAccountName over 20 characters.

    Liked by 1 person

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.