Output AD infomration

Welcome Forums General PowerShell Q&A Output AD infomration

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
6 months, 2 weeks ago.

  • Author
    Posts
  • #99880

    Participant
    Points: 0
    Rank: Member

    I have a list of displaynames in a file (userlist.txt). I have a piece of powershell code that will loop thru that list check if that display name exists in AD. The output is either User does exists in AD or User does not exist in AD. Below is my code.

    $Users = Get-Content "c:\AD Scripts\users.txt"
    foreach ($User in $Users) {
    if (Get-ADUser -Filter {displayName -like $User}){
    Write-Host "$User does Exist in AD!"

    }
    else {
    Write-Host "$User does not exist in AD"

    }

    }

    I would like to output this to a csv file displaying the displayname AND samAccountName. If the display does not exists in AD, I would like to output N/A in the samAccountName field of the csv file. Any help is appreciated.

  • #99886

    Participant
    Points: 21
    Rank: Member

    So a few things I would do here.

    1. not use write-host for your output method – if you want to write screen may i kindly suggest using write-output – makes life easier all around. Write-host doesn't always follow all the rules with outputting so it makes it easier in the long run to use write-output.

    2. utilize try/catch to determine if your ad account is found and create objects based on what it found

    I included below a sample of what this could look like. There are a few other permutations that this could take but this is a simple example building upon the work you already did.

    Please let me know of any questions I can clarify!

    $Users = Get-Content "c:\AD Scripts\users.txt"
    $data = foreach ($User in $Users)
    {
    	try
    	{
    		Get-ADUser -filter { displayName -like $user } -ErrorAction Stop | foreach-object{
    			New-Object -TypeName System.Management.Automation.PSObject -Property @{
    				"SamAccountName"   = $($_.samaccountname)
    				"DisplayName"	   = $($user)
    			}
    		}
    	}
    	catch
    	{
    		New-Object -TypeName System.Management.Automation.PSObject -Property @{
    			"SamAccountName"   = "N/A"
    			"DisplayName"	   = $($user)
    		}
    	}
    }
    $data | Export-Csv -NoTypeInformation -Path "C:\Ad Scripts\Users.csv"
    
  • #99889

    Participant
    Points: 0
    Rank: Member

    Thanks for your quick reply. When hen I changed this code, it is missing data in the CSV. $Users file has 194 names. However, the $data output is only containing about 84 records. It is not populating any records with NA (apparently those are being left out).

  • #99916

    Participant
    Points: 159
    Helping Hand
    Rank: Participant

    The -Filter either returns a found record or nothing (NULL), it does not produce an error, which is why nothing is returned for the users that don't have a matching name. If you use -Identity $user, then it would throw an error and a try\catch could be leveraged, but Identity would be better with SamAccountName or LDAP Path. You can try something like this to find the user and if they are not found generate a matching object.

    Import-Module ActiveDirectory
    
    $Users = Get-Content "c:\AD Scripts\users.txt"
    
    $results = foreach ( $User in $Users ) {
    
        $user = Get-ADUser -filter { DisplayName -eq $user } | Select Name, SamAccountName, @{Name='Status';Expression={'Found'}}
    
        if ( $user ) {
            $user
        }
        else {
    		New-Object -TypeName System.Management.Automation.PSObject -Property @{
    			"SamAccountName" = $null
    			"Name"	         = $user
                "Status"         = 'Not_Found'
            }
        }
    }
    
    $matchedUsers = $results | Where {$_.SamAccountName}
    #or
    #$matchedUsers = $results | Where {$_.Status -eq 'Found'}
    
    $missingUsers = $results | Where {!($_.SamAccountName)}
    #or
    #$missingUsers = $results | Where {$_.Status -eq 'Not_Found'}
    
    
    $results | Export-Csv -NoTypeInformation -Path "C:\Ad Scripts\Users.csv"
    
    • #99925

      Participant
      Points: 21
      Rank: Member

      You make a good point Rob – I tried it in my test environment with samaccount instead of displayname – and forgot to adjust for the filter. Doh – sometimes the quick and dirty has issues. I skipped over the displayname since I typically don't use that to look up – thanks for catching my error there.

The topic ‘Output AD infomration’ is closed to new replies.