Get-ADUser with Import CSV - Unexpected Result

I have a .csv file with several columns, one of which is Employeeid. I’m importing the CSV file and checking AD for any records that don’t have an entry in the Employeeid field. The following returns the correct result, which is one record:

    Import-CSV D:\Data\Allen\HR_EMPLID_EXP.csv -Delimiter "|" | Select -Last 2  |
ForEach-Object {
    $Employeeid = $_.Employeeid
    Get-ADUser -Filter "Employeeid -eq '$Employeeid'" -Properties GivenName,sn | 
        Select-Object -Property GivenName,sn

However, the following generates ALL records in AD that don’t have anything in employeeid when the return should be one record. So, it appears that the following is not just comparing against the last 2 rows in the CSV:

    Import-CSV D:\Data\Allen\HR_EMPLID_EXP.csv -Delimiter "|" | Select -Last 2  |
ForEach-Object {
    $Employeeid = $_.Employeeid
    Get-ADUser -Filter "Employeeid -ne '$Employeeid'" -Properties GivenName,sn | 
        Select-Object -Property GivenName,sn

Any help as to why I cannot just get the expected one result from the last bit of code is appreciated.

A query like this returns ALL accounts not having the provided employeeid … and that would be either ALL account when the employeeid does not exist or ALL accounts minus one when an account with the given employeeid exists. :wink:

BTW: I corrected the formatting of your code. If you do it by hand you have to use three consecutive backticks - not single quotes. Or you use the editor option …

… to post code, sample data, console output or error messages you can use the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

1 Like

Thank you, @Olaf and sorry about the code posting. What is the proper way of comparing the Employeeid in the CSV against AD to list accounts with nothing in the AD Employeeid field?

I would have searched for “powershell check aduser with blank employeeid

One of the first hits would be this:

Yup @Olaf I did see that when I searched. It’s a little different than what I’m attempting. Basically, I want to take a list of names from a .CSV, check if the employeeid is present. That example from the search is looking in the whole AD. But in my attempt, as shared in my OP, the results of users with no employeeid are returned for my whole AD rather than just the list of names I’m pulling from the CSV.

If I got you right you should use the same check as before and just us try catch error handling to gracefully handle the error you get when no account is found with the given employee id.

I included the Employeeid field in the return of results and the ones that are returned when using the “-ne” operator HAVE and entry for the employeeid AND it equals what is in my source file. Very odd. I tried changing -ne to -notequal but received a syntax error.

If you want to get a list from AD of employees that are in a csv and that have no employeeid in AD, then I would recommend

Step 1 - get all AD accounts that don’t have an employeeid

$aduserlist=Get-ADUser -Filter "employeeid -notlike '*'"

Step 2 - extract from the AD list any user that is in the csv

$csvdata = Import-CSV D:\Data\Allen\HR_EMPLID_EXP.csv -Delimiter "|"

$aduserlist | Where-Object SamAccountName -in $csvdata.samaccountname