title: How to move multiple users to multiple OUs importing users from CSV and filtering by department
body: <p>New to powershell and scripting in general. Trying to improve automation in our onboarding process, we have to move multiple user accounts to multiple OUs every couple of months.</p>
  
  <pre><code>Import-Module ActiveDirectory
  $dept1 = "OU=Path1,DC=SOMEWHERE,DC=OUTTHERE"
  $dept2 = "OU=Path2,DC=SOMEWHERE,DC=OUTTHERE"
  Import-Csv "C:\Scripts\Incoming.csv" | ForEach-Object {
      $samAccountName = $_."samAccountName"
  Get-ADUser -Identity $samAccountName -Filter {Department -eq "Dept1"} | Move-ADObject -TargetPath $dept1
  Get-ADUser -Identity $samAccountName -Filter {Department -eq "Dept2"} | Move-ADObject -TargetPath $dept2
  }
  </code></pre>
  
  <p>This actually moves ALL users with the department marked into the path I have set.. but I only want it to look at those users in the csv and then filter their departments from AD - not the CSV. I feel like I'm missing a step but I've searched everywhere and tried the get-help. I feel like I need to get the identity, then search/filter against something else but I'm not quite sure how. Would appreciate any help.</p>
  
  <p><strong>edit</strong></p>
  
  <p>Okay, if I run the following:</p>
  
  <pre><code>
  Get-ADUser -Filter {Department -eq "Dept1"} -Properties Department
  
  </code></pre>
  
  <p>It returns everyone that fits that property but how do I compare those to the $samAccountName and ONLY try to move those accounts or run the commands against the accounts on the list? When I ran the second half of the command it tried to move them all and failed.</p>
  
  <pre><code>
  Move-ADObject $samAccountName -Target $dept1
  
  </code></pre>
  
  <p>I feel dumb.</p>