AD Name Check – Export

#Search Active Directory for any accounts that match the first and last names listed in the Users_to_Check.csv file and exports them in a spreadsheet

$csvFilePath = 'C:\Users\Public\Documents\Users_to_Check.csv'

 

$outputFilePath = 'C:\Users\Public\Documents\Name_Check_Results.csv'

 

$csvFile = Import-Csv -Path $csvFilePath

 

$userDetails = @()

 

ForEach ($row in $csvFile) {

$firstName = $row.FirstName

$lastName = $row.LastName

 

$user = Get-ADUser -Filter "GivenName -eq '$firstName' -and Surname -eq '$lastName'"

 

if ($user) {

$userDetails += $user

} else {

Write-Output "No user found with first name '$firstName' and last name '$lastName'"

}  

}

$userDetails | Export-Csv -Path $outputFilePath