• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1241
  • Last Modified:

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

0
WWUSteve
Asked:
WWUSteve
  • 2
1 Solution
 
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

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now