How to export certain users from AD through matching users in csv file, in PowerShell?

SysAdmin 151 Reputation points
2023-09-14T17:37:46.4366667+00:00

Hi

I have a list of users in a CSV file (600 user), and I want to use this CSV file to match it with AD users in order to extract/export from AD the users who matches with users in the CSV file using PowerShell, I will export each user with its properties information into one new CSV file.

Those users in the CSV file are already exist in AD and the key matching between the users in the CSV file and users in the AD is username, all users in CSV file have a username but no other information, so I need to export those users with their information's from AD.

After that I will import the new CSV file to a different AD

Please, how to export the users using loop through CSV file and then export the results to a new CSV file.

Then import the new CSV file into the different AD?

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
    2023-09-15T19:26:20.91+00:00

    Since you're using the name of the user instead of a property that's guaranteed to be unique, you should check to be sure the Get-ADUser returns only a single object.

    This may be what you need:

    $props = "samaccountname","name","givenname","surname","displayname"    # these are the names of the properties you want to export
    Import-Csv c:\junk\my.csv |
        ForEach-Object{
            try{
                $u = Get-ADUser -Filter "name -eq '$($_.username)'"
                if ($u){
                    if ($u.count -gt 1){
                        Throw "Multiple matches for name '$($_.username)"
                    }
                    $n = Select-Object -InputObject $u $props
                    $n.name = 'T' + $n.name
                    $n                          # emit the new row
                }
            }
            catch{
                # report an unmatched or duplicate name here
            }
        } | Export-CSV c:\junk\another.csv -NoTypeInformation
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-09-14T19:02:22.4066667+00:00

    If the users have only a username but no other information, why do you want to export their information???

    You don't give any details about the column names in your CSV, or what they contain.

    You can use this to start your own script:

    $props = "samaccountname","name","givenname","surname","displayname"    # these are the names of the properties you want to export
    Import-Csv c:\junk\my.csv |
        ForEach-Object{
            try{
                Get-ADUser -Identity $_.samaccountname -ErrorAction STOP |
                    Select-Object $props
            }
            catch{
                # report an unmatched samaccountname here
            }
        } | Export-CSV c:\junk\another.csv -NoTypeInformation
    
    1 person found this answer helpful.

  2. SysAdmin 151 Reputation points
    2023-09-15T11:27:19.38+00:00

    Hi @Rich Matheisen

    Thank you for help

    The excel sheet (CSV) contain only 2 columns (full name and username), their usernames are number based no letters

    and I need to export those users from AD with their properties and custom attributes values maybe 4 or 6 columns (for example: username, full name, Id number, mobile number, department, organization, email, etc. ), after I export to new CSV file then I will import it to a totally different AD.

    in the new CSV file I want to modify their username by adding letter "T" in the beginning of every username, I know how to do it from excel sheet but instead, I would really like to do it from PowerShell

    Please, how to do modification on username column and import csv to AD in "all in one code"

    as for the code above is it for exporting new CSV with users properties?


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.