none
Get-ADUser from CSV RRS feed

  • Question

  • I am trying to create a CSV of SAM account names by looping through another CSV containing Employee IDs using the following code:

    $datacsv = Import-Csv 'EIDexport.csv'
    
    
    foreach($EID in $datacsv)
    { 
        Get-ADUser -ResultPageSize 900 -Filter "EmployeeID -eq $($EID.EmployeeID)" -Properties * | Select Name,SAMAccountName | Export-Csv 'EID2SAM.csv'  -Append
    
    } 

    The source file has over 880 entries.  The results are always under 200.  Any ideas what the problem might be?

    Monday, January 9, 2017 6:53 PM

Answers

  • P.S. - if there are extra space characters in AD then the match will not be made.


    \_(ツ)_/

    • Marked as answer by art Alexion Monday, January 9, 2017 9:11 PM
    Monday, January 9, 2017 7:26 PM

All replies

  • First lets fix your script:

    Import-Csv EIDexport.csv|
    	ForEach-Object{
    		Get-ADUser -Filter "EmployeeID -eq '$($_.EmployeeID)'" 
    	} | 
    	Select Name, SAMAccountName | 
    	Export-Csv EID2SAM.csv -NoType

    You are returning one item at a time so it will be slow.  If the EIDs in the file are not in AD then the item will not be present in the output.

    Compare the files and extract the missing IDs then test a couple manually to see why they are missing.


    \_(ツ)_/



    • Edited by jrv Monday, January 9, 2017 7:24 PM
    Monday, January 9, 2017 7:15 PM
  • P.S. - if there are extra space characters in AD then the match will not be made.


    \_(ツ)_/

    • Marked as answer by art Alexion Monday, January 9, 2017 9:11 PM
    Monday, January 9, 2017 7:26 PM
  • Thanks again, jrv!

    Your second clue helped me solve the problem.  I was using Excel to create the original CSV.  Excel treated the employee ID field as a number instead of text, thereby removing leading zeros.  This resulted in mismatches in any EID with leading zeros.

    Monday, January 9, 2017 8:20 PM