Glenn Maxwell 10,186 Reputation points

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.


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 Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,505 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,400 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,992 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,406 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 Reputation points

    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,  
                                @{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