Move AD Users to different OU with employeeID attribute

Hello, I need some help writing a powershell script to move AD users from one OU to another. Below is a script I have used in the past when I'm importing a .csv off of the Username. How could I change this script to pull the employeeID field? Thanks!

Import-module ActiveDirectory

# Specify target OU.
    $TargetOU = "OU=Alumni,DC=domain,DC=com"

# Read user sAMAccountNames from csv file (field labeled "Name").
    Import-Csv -Path C:\Users\Tester\Desktop\tempalumni.csv | ForEach-Object {

# Retrieve DN of User.
    $UserDN = (Get-ADUser -Identity $_.Name).distinguishedName

# Move user to target OU.
    Move-ADObject -Identity $UserDN -TargetPath $TargetOU
}

Open in new window

WWUSteveAsked:
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.

footechCommented:
I'm really not understanding your scenario.  How is the employeeID related to moving users to another OU?  What users are you looking to modify?  Pull the employeeID field from where?

To include the employeeID (or any other that isn't returned by default) in the properties returned by Get-ADUser, you have to specify it with the -properties parameter.
Get-ADUser someuser -properties employeeID
0
WWUSteveAuthor Commented:
I'll try to explain more clearly what I'm trying to accomplish.

In our AD environment, all of our users have the employeeID attribute filled out. I'm trying to take a CSV with a header and the ID numbers I want to move (I have the list of ID numbers) and then pull them from the many OU's into the Alumni OU defined in the script. I have ran the script with the sam account names and it works perfectly.

Does that help?

Thanks!
0
footechCommented:
Assuming your. CSV looks like this:
employeeID
321
3234
432
7657

Then you could use the following.  Remove the -whatif parameter from Move-ADObject to run for real.
$TargetOU = "OU=Alumni,DC=domain,DC=com"
$IDs = Import-CSV ids.csv | Select -ExpandProperty employeeID
Get-ADUser -filter * -Properties employeeID | Where { $IDs -contains $_.employeeID } | 
 Move-ADObject -TargetPath $TargetOU -WhatIf

Open in new window

0

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
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.