locked
get samaccountname from csv of employeeid RRS feed

  • Question

  • Hi,

    I have a CSV file that contains employeeID

    I'm trying to create a script to go through these employeeIDs and check them against the AD. if match return the samaccountname if not then write the employeeID and "write not found"

    so the imoported CSV file is

    employeeID1

    employeeID2

    employeeID3

    then the exported CSV file will be

    employeeID1  SamAccountName1

    employeeID2  Not Found

    employeeID3  SamAccountName2

    I've started with the below script but its not working and i cant find how to use the "If" statement and else to export the "notfound" value.

    any help will be appreciate it


    $Users = Import-Csv C:\scripts\MatchNavAD.csv -Encoding UTF7
    foreach ($User in $Users)
        {
         Get-ADUser -Filter "EmployeeID -eq $($userID.EmployeeID)" -Properties SAMAccountName
    }

    • Edited by ITinNeed Wednesday, October 25, 2017 5:24 AM
    Wednesday, October 25, 2017 5:22 AM

All replies

  • Use "if" to test the results.

    Import-Csv C:\scripts\MatchNavAD.csv |
    	ForEach-Object{
    		if($user = Get-ADUser -Filter "EmployeeID -eq '$($_.EmployeeID)'"){
    			[pscustomobject]@{EmployeeID=$_.EmployeeID;SamAccountName=$user.SamAccountName}
    	    }else{
    			Write-Host 'Not Found'
    	    }
    	} |
    	Export-Csv <output file>


    \_(ツ)_/


    Wednesday, October 25, 2017 8:58 AM