Solved

Add many users to many different groups in Active Directory using Powershell, reading from a tab-delimited file.

Posted on 2009-05-08
3
696 Views
Last Modified: 2012-05-06
I need to assign many users to many groups in Active Directory using a tab-delimited TXT file as the source. I would prefer to use Powershell as I am trying to learn it. Using Powershell v1.0 on Windows XP Sp2. An example of the file I would use is attached.

Much thanks!
Import-CSV.txt
0
Comment
Question by:cnrlzen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24352520

Morning :)

Weekends are a bit quiet, and very few of us answer PowerShell questions hence the late reply.

I recommend grabbing the Quest PowerShell CmdLets from here:

http://www.quest.com/powershell/

They're free and will make this task a lot simpler. It is, of course, entirely possible to do this in native PowerShell, just longer.

One more note. You'll find that Comma Delimited formatting is easier to deal with than Tab Delimited. With Comma Delimited you can use Import-CSV and refer to each line by the name in the header.

Anyway, Examples...

These are rather verbose, unnecessarily so, but I figured demonstrating was more important than fitting it onto a line.

Chris
# Using Get-Content to read the text file
$FileContent = Get-Content "Test.txt"
 
# Removing the header line by getting elements 1 to end from the array
# Count uses 1 as the base, where an array uses 0; hence "Count - 1"
$FileContent = $FileContent[1..$($FileContent.Count - 1)]
 
# Looping through the Content
ForEach ($Line in $FileContent) {
  # Splitting the line into each element `t is Tab.
  $LineArray = $Line.Split("`t")
 
  # Getting the elements
  $UserDN = $LineArray[0]
  $GroupDN = $LineArray[1]
 
  # Using Add-QADGroupMember to add the member to the group
  Add-QADGroupMember $GroupDN -Member $UserDN -WhatIf
}
 
# If you had a CSV File as input you could do this in one line
# In the example below % is an alias for ForEach-Object
 
Import-CSV "Test.csv" | %{ Add-QADGroupMember $_.memberOf -Member $_.distinguishedName }

Open in new window

0
 

Author Closing Comment

by:cnrlzen
ID: 31579683
That's great - I used a reformatted CSV file and it worked well. How can I log any errors and the successes of each action to a log file so I know where failures might occur?
0

Featured Post

Office 365 Training for Admins

Learn how to provision tenants, synchronize on-premise Active Directory, and implement Single Sign-On with these master level course.  Only from Platform Scholar

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question