Powershell Script to Move users from one OU to Another

I need a powershell script that takes input from a CSV of user samaccount names and moves them from one ou to another .
gd6627Asked:
Who is Participating?
 
YZlatConnect With a Mentor Commented:
try something like that

 
$Allusers = import-csv -path c:\mycsvfile.csv 
$ou = "OU=Test,DC=YourDomain,DC=com"
foreach ($user in $Allusers) {
	Move-QADObject $user -To $ou
}

Open in new window


But first add this snap-in: Quest.ActiveRoles.ADManagement
0
 
Kris MontgomeryCommented:
Here is a starting point:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/f7cd1004-2f23-4ce6-b090-d5679df5610c

You might also consider using PowerGUI with the AD powerpack:  http://www.powergui.org/index.jspa

Good luck!
mug
0
 
gd6627Author Commented:
I have power gui and i loaded quest cmdlts need to put it together is where am getting stuck
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
Bryan ButlerCommented:
You could use import-csv:
note: if you don't have a header row, use something like: Import-Csv .\test.csv -header ("col1","col2","username")
note: this script assumes you have a "username" column specified in the header

Import-Csv c:\scripts\users.csv | foreach {
 
# Finding the location of the user account Andre:
$Root = [ADSI]''

$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=user)(DisplayName= $_.username))"
$User = $searcher.findone()

# Binding the user account to $AUser and the OU to move to to $MovetoOU
$ADSPath = $User.Properties.adspath
$MoveToOU = [ADSI]("LDAP://OU=NewOU,DC=mydomain,DC=com")
$AUser = [ADSI]("$ADSPath")

# Command to Do the actual move
$AUser.PSBase.moveto($MoveToOU)

}
0
 
gd6627Author Commented:
thank you
0
 
Bryan ButlerCommented:
Any luck with this?
0
 
gd6627Author Commented:
yes that worked
0
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.

All Courses

From novice to tech pro — start learning today.