Get-ADUser

Glenn Maxwell 12,876 Reputation points
2022-08-25T09:46:43.517+00:00

Hi all i have a csv file with employee ids in the below format.
i want to import the csv and find out whether these emp id exists in AD and if they exits i found to export their below information and if not i want to add employee id not found in csv file.

id
123
124
125
126

Will the below syntax work

import-csv c:\temp\input.csv | ForEach {Get-ADUser -filter {employeeID -eq $id -or employeeNumber -eq $id} -Properties DisplayName,GivenName,Surname,SamAccountName,EmailAddress,Userprincipalname,title,Office,description,co,personalTitle,DepartmentNumber,employeeNumber,employeeType,manager |Select Surname,GivenName,DisplayName,SamAccountName,EmailAddress,Userprincipalname,title,Office,employeeNumber,employeeType,description,co,personalTitle,@{Name='DepartmentNumber';Expression={[string]::join(";", $($_.DepartmentNumber))}},@{name='Manager';expression={$_.manager -match '(?<=CN=).+?(?=,)'|Out-Null;$Matches.Values}}  | Export-CSV -Path C:\temp\output.csv -Notypeinformation  


  
Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-08-25T14:56:02.473+00:00

    I'm not a big fan of using long lists of values in a cmdlet, and I'm dead set against the use of "one-liners". Why obfuscate your meaning in a jumble of run-on pipes and punctuation?

    But, to answer your question, your code will work.

    Additionally, I think this is a lot clearer (it's almost the same code, just reformatted):

    $props = "DisplayName,GivenName,Surname,SamAccountName,EmailAddress,Userprincipalname,title,Office,description,co,personalTitle,DepartmentNumber,employeeNumber,employeeType,manager" -split ","  
    import-csv c:\temp\input.csv |   
        ForEach-Object {  
            Get-ADUser -filter {employeeID -eq $id -or employeeNumber -eq $id} -Properties $props |  
                Select-Object   Surname,GivenName,DisplayName,SamAccountName,EmailAddress,Userprincipalname,  
                                title,Office,employeeNumber,employeeType,description,co,personalTitle,  
                                @{Name='DepartmentNumber';Expression={[string]::join(";", $($_.DepartmentNumber))}},  
                                @{name='Manager';expression={$_.manager -match '(?<=CN=).+?(?=,)'|Out-Null;$Matches.Values}}  
        }| Export-CSV -Path C:\temp\output.csv -Notypeinformation  
      
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.