Move Users Accounts using Filter and Path from CSV

Hi 

Im trying to move user accounts based on a set of filters on a csv file to the appropriate OU on that same file. I want to grab all users in AD that match the teachers name and grade level for each row, and move them to the OU listed for that same row. 

Here is how the date is configured on the CSV file

School

Grade

Teacher ADOU
QCV 0       Jane Doe OU=Teacher1,OU=Grade 0,DC=students,DC=local

Here's the script Im using:

import-module activedirectory
$mappings = Import-Csv -Path "C:\studentsAccounts\Student_AD_ImportSorted.csv"

foreach ($map in $mappings){
    $grade=$map.'grade'
    $teacher=$map.'Teacher'
    $OU=$map.'ADOU'
    
    Get-ADUser -server "ho-sb-dc03" -Filter {(Department -eq "$grade") -and (Title -eq "$teacher")} | Move-ADObject -Server "ho-sb-dc03"  -TargetPath $OU
}


When i run it, i get an error about the $OU variable being null.

Move-ADObject : Cannot validate argument on parameter 'TargetPath'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
At C:\studentsAccounts\MoveNewStudents.ps1:9 char:149
+     Get-ADUser -server "ho-sb-dc03" -Filter {(Department -eq "$grade") -and (Title -eq "$teacher")} | Move-ADObject -Server "ho-sb-dc03" -TargetPath <<<<  $OU
    + CategoryInfo          : InvalidData: (:) [Move-ADObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.MoveADObject
 

August 28th, 2015 3:41pm

try this

import-module activedirectory
$mappings = Import-Csv -Path "C:\studentsAccounts\Student_AD_ImportSorted.csv"

foreach ($map in $mappings){
    $grade=$map.grade
    $teacher=$map.Teacher
    $OU=$map.ADOU

Get-ADUser -Filter {(department -eq $grade) -and (title -eq $teacher)} | Move-ADObject -TargetPath $OU}

I don't include the '-server' since it is the same Domain controller.

Free Windows Admin Tool Kit Click here and download it now
August 29th, 2015 9:14am

I want to make sure we understand the problem. The department attribute of the user will have the grade level, such as 0. The title attribute of the user will have the name of a teacher, such as "Jane Doe". All users (students) with department "0" and title "Jane Doe" will be moved to "ou=Teacher1,ou=Grade0...".

A few comments, since I once supported schools. We placed all students in OU=Students, but we created groups for each grade because groups allowed us to assign permssions (one shared folder per grade for example, perhaps different password polices for Kindergarten). When we placed students in groups named after the grade, we had to move them into a different group each summer. You would do the same if you group them in OU's. Instead, we named the groups after their expected graduating year. We only needed to possibly change the permissions of the group. We only moved a few exceptions, plus create a new group for the new graduating year each summer. We had another group for each homeroom (the same as your teacher).

August 29th, 2015 11:23am

Hi, 

How's your powershell going on? I hope my powershell answered based on your problem encountered.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 8:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics