Script to Add users to Groups specified into txt file (Active Directory)

Hi guys!
I need to add users into groups by creating these groups if they doesn't exist yet.

I would start from a txt file containing users and groups like that:

Surname Name#Administrative
Surname2 Name2#Sell
Surname3 Name3#Administrative

I need to read each row for each user and each group, create the group if it doesn't exist in AD by adding a suffix "Office_" (so the new group will be "Office_Administrative", following the txt example I first wrote) and add the user to the group.

Thx in advance!
LVL 12
Max LoiSr. System EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Krzysztof PytkoSenior Active Directory EngineerCommented:
OK, first we need to get some more details before we can start. Where these users and groups should be created (which OU) ?

Then we can start using for that Microsoft DS Tools or Quest PowerShell module for AD.

Thanks in advance for more information

Max LoiSr. System EngineerAuthor Commented:
Hi Siek, thx for your reply.
The OU will be "OU=Test,DC=Mydomain,DC=local"

I've installed the Quest PowerShell module for AD yet :)
Krzysztof PytkoSenior Active Directory EngineerCommented:
OK, give me a little time and I will post script for that :)

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Max LoiSr. System EngineerAuthor Commented:
I'm near to the solution, I did the script above...

The script runs quite fine, but I get some error when the group yet exist or there is a "duplicated user in AD" (ie: "john smith" and "smith john" and "john smith jr.")

I need to put this users and relative groups into a logfile so I could be able to manage them by hands. No matter if the script gives me an error when a group yet exists, because I'm also able to add users into it till the end of my file and also create new groups that does not exist yet.

$filename = "your_path\filename.txt"
$OU = "OU=Test,DC=mydomain,DC=local"
$GroupType = "Security"
$GroupScope = "Universal"
$suffix = "Office_"

ForEach ($Entry in (Import-CSV $filename)) {
$Name = $Entry.Name
$Gruppo = $Entry.Group
$checkGroup = Get-QADGroup -SamAccountName $Gruppo

if ($checkGroup -eq $null)
	 Write-Host "Creating new Group..." 
	 $NewGroup = "$suffix$Gruppo"
	 New-QADGroup -Name $NewGroup -SamAccountName $NewGroup -ParentContainer $OU -GroupType $GroupType -GroupScope $GroupScope
	 $Gruppo = $NewGroup

	 $b = Get-QADGroupMember -Identity $Gruppo |Select-String $Name
if ($b -eq $null)
 Add-QADGroupMember $Gruppo -Member $Name

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Krzysztof PytkoSenior Active Directory EngineerCommented:
Great, so try with "-ErrorAction SilentlyContinue"

Max LoiSr. System EngineerAuthor Commented:
Thx Krzysztof, but I really don't know if this could be useful for me, because my script doesn't stop on error, but it still continue working until the end of the loop "foreach".

Instead, I'm doing some test and I got some problem by redirecting the errors I get into a log file, to understand which groups and users fail during the loop. I'm also having another kind of problem: it seems that sometimes this script add the first user it founds into the third group (that is "of another user"), and I don't know why...

Max LoiSr. System EngineerAuthor Commented:
I found the solution by myself
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.