Friday, 28 March 2008

Add Users to an AD group from a CSV file

So I had a text file containing data in the following format:

basically Novell usernames.

These accounts have equivalent usernames in Active Directory in the format




ie. everything after the first dot is removed.

The task is to open up the text file, convert the Novell usernames to the AD format and then add each user to a group. With a bit of help from a post of Richard Siddaway's (!43CFA46A74CF3E96!822.entry) the below script does the job.

(As usual the Quest AD cmdlets are required)

# Open the text file, split out each username at the first '.' and keep the first part of each split.
$users = Get-Content C:\Scripts\UserList.csv | %{$_.split(".")[0]}

# For each username get the user's AD DN and add the user to the specified group.
foreach ($a in $users)
Get-QADUser -Identity $a | Add-QADGroupMember -Identity 'domainname\groupname'

No comments: