Share via


get samaccountname from csv of employeeid

Question

Wednesday, October 25, 2017 5:22 AM

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
}

All replies (1)

Wednesday, October 25, 2017 8:58 AM

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>

\(ツ)_/