Get-User from CSV and export $null to CSV

Patrick Mullen 1 Reputation point


I'm trying to perform what I thought would be a simple command. I want to import a csv of users (first/last name) and export the errors to a csv. The purpose is so when I get requests to add hundreds of users to a DL I can run the csv through PS, export the user accounts that don't exist with that first/last name and send the errors back to the business so they can do the leg work of looking up the correct user names.

I searched google for hours and couldn't find what I was looking for.

Thank You

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,404 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points

    Well, that makes for shorter code.

    If I assume that duplicate AD users aren't a problem (although I think will be) and that a mailNickName is placed into the "FirstName" column of the CSV then you should be able to use that to shorten the list of "bad" users by adding an additional condition to the filter..

    Import-CSV c:\junk\newusers.csv |
        ForEach-Object {
            [array]$x = Get-ADUser -Filter "(givenname -eq $($_.firstname) -and surname -eq $($_.lastname)) -or (mailNickname -eq $($_.firstname))"
            if ($x.count -eq 0) {
                    FirstName = $_.firstname
                    LastName  = $_.lastname
                    Error     = "no user exists that matches either the first and last name, or whose mailNickname matches the first name"
         } | Export-Csv c:\junk\badusers.csv -NoTypeInformation
    1 person found this answer helpful.

  2. Rich Matheisen 45,111 Reputation points

    Here's a possible skeleton:

    $badusers = @()
    $u = Import-CSV c:\junk\newusers.csv
    ForEach ($user in $u){
        $errors = @()
        [array]$x = Get-ADUser -Filter "givenname -eq $($user.firstname) -and surname -eq $($user.lastname)"
        if ($x.count -gt 1 ){
            $errors += "there are multiple users in the AD with names that match both first and last names"
        elseif ($x.count -eq 1){
            $errors += "a user already exists with the same first and last name"
        elseif ($x.count -eq 0){
            # do nothing -- there is no match on first and last names
        if ($errors.count -gt 0){
            $errors |
                    $badusers += [PSCustomObject]@{
                                    FirstName = $user.firstname
                                    LastName  = $user.lastname
                                    Error     = $_
        # process the "good" user here
    $badusers | Export-CSV c:\junk\badusers.csv -NoTypeInformation

    I think the dependence on having a unique first/last name as a means of identifying an AD user is not the best approach, especially if you're managing "hundreds" of new users at a time (which seems to imply that you have multiple thousands of users in the AD). There are absolutely going to be duplicates.

    0 comments No comments

  3. Andreas Baumgarten 98,606 Reputation points MVP

    Hi @Patrick Mullen ,

    maybe this is working for you:

    Import-Module ActiveDirectory   
    $missingUser = @()  
    Import-Csv -Path 'c:\temp\UserFile.csv' | ForEach-Object {  
        $adUser = Get-ADUser -Filter "GivenName -eq '$($_.FirstName)' -and sn -eq '$($_.LastName)'"  
        if (!$aduser) {  
            $missingUser = $missingUser + "$($_.FirstName),$($_.LastName)"  
    "FirstName,LastName" | Out-File -FilePath 'c:\temp\MissingUser.csv' -Encoding utf8  
    $missingUser | Out-File -FilePath 'c:\temp\MissingUser.csv' -Encoding utf8 -Append  




    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Andreas Baumgarten