Powershell Help

Hello Experts,

We currently have a list of users in a CSV file and they have only provided me with the GivenName and Surname. They would like me to do a search in AD to see if the account exists, and if it does provide the sAMAccountName and Email Address (if one). If the account does not exist write-host Account not found.

I typically use the sAMAccountName with a Write-host saying if the account exists or not but they are requiring a little more information and providing me with different attributes.

Any help would be appreciated!
LVL 53
Will SzymkowskiSenior Solution ArchitectAsked:
Who is Participating?
Jamie McKillopConnect With a Mentor IT ManagerCommented:

Try this:

Import-Csv c:\import.csv | % {
	$Firstname = $_.givenname
	$Lastname = $_.surname
	$user = Get-User -Filter {firstname -eq $firstname -and lastname -eq $lastname}
	foreach ($item in $user) {	
		if ($item -eq $null) {
			Write-Host "$($firstname) $($lastname) Not Found"
		} else {
			Write-Host "$($item.samaccountname) $($item.windowsemailaddress)"

Open in new window

Will SzymkowskiSenior Solution ArchitectAuthor Commented:
Thanks for the response. Worked like a charm.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.