User account search script

I've built the following User search based on first name and last name for AD.  The way it's written, it only returns the first result. How can I re-write this to return all the results?

Write-Host "Please enter a first name to search: " -NoNewline -ForegroundColor Yellow
	$first = Read-Host
	Write-Host "Please enter a last name to search: " -NoNewline -ForegroundColor Yellow
	$last = Read-Host 
	$fullname = $first + "*" + $last + "*"
	$username = get-aduser -filter{displayname -like $fullname} -properties * | select -expand samaccountname
	
	if ((get-aduser -filter{displayname -like $fullname}) -ne $null)
	{	
	$fname = get-aduser -identity $username -pr * | select -ExpandProperty givenname
	$lname = get-aduser -identity $username -pr * | select -ExpandProperty surname
	$office = get-aduser -identity $username -pr * | select -ExpandProperty office
	$description = get-aduser -identity $username -pr * | select -ExpandProperty description
	$notes = get-aduser $username -properties info | Select-Object -ExpandProperty info
	$enabled = get-aduser $username -pr * | select -ExpandProperty enabled
	Write-Host "First Name: " -NoNewline -ForegroundColor Green
	Write-Host $fname -ForegroundColor Gray
	Write-Host "Last Name: " -NoNewline -ForegroundColor Green
	Write-Host $lname -ForegroundColor Gray
	Write-Host "Username: " -NoNewline -ForegroundColor Green
	Write-Host $username -ForegroundColor Gray
	Write-Host "Account Enabled: " -NoNewline -ForegroundColor Green
	Write-Host $enabled -ForegroundColor Gray
	Write-Host "Location: " -NoNewline -ForegroundColor Green
	Write-Host $office -ForegroundColor Gray
	Write-Host "Account Description: " -NoNewline -ForegroundColor Green
	Write-Host $description -ForegroundColor Gray
	Write-Host "Account Notes: " -ForegroundColor Green
	Write-Host $notes -ForegroundColor Gray
	}
	else {
	Write-Host "User $fullname does not exist in AD." -ForegroundColor Red 
	}

July 7th, 2015 2:33pm

Few problems I see, you have the following line

$username = get-aduser -filter{displayname -like $fullname} -properties * | select -expand samaccountname

This should be an array of samAccountNames since more than one user can have a displayname that are the same. You then go on to say

if ((get-aduser -filter{displayname -like $fullname}) -ne $null)

Which is the same as the first line I posted, I would check to see if $username is $null. Since this is an array of samaccountnames, you will need to loop through it.

The other issue is do NOT do -Prop * unless for some reason you need every single property, which looking at the script you do not. You then go on to make multiple Get-ADUser calls, but selecting and expanding different properties, I think this is wasteful and not all properties need to be expanded.

Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 2:54pm

This is much cleaner

$first = Read-Host "Please enter a first name to search: "
$last = Read-Host "Please enter a last name to search: "
$fullname = $first + "*" + $last + "*"
$usernames = get-aduser -filter{displayname -like $fullname} -properties * | select -expand samaccountname
	
if ($usernames -ne $null)
{
  foreach($user in $usernames)
  {
    $u = Get-ADUser -Identity $user -Properties Office, Info, Enabled, Description
    Write-Host "First Name: " -NoNewline -ForegroundColor Green
    Write-Host $u.GivenName -ForegroundColor Gray
    Write-Host "Last Name: " -NoNewline -ForegroundColor Green
    Write-Host $u.Surname -ForegroundColor Gray
    Write-Host "Username: " -NoNewline -ForegroundColor Green
    Write-Host $u.SamAccountName -ForegroundColor Gray
    Write-Host "Account Enabled: " -NoNewline -ForegroundColor Green
    Write-Host $u.Enabled -ForegroundColor Gray
    Write-Host "Location: " -NoNewline -ForegroundColor Green
    Write-Host $u.Office -ForegroundColor Gray
    Write-Host "Account Description: " -NoNewline -ForegroundColor Green
    Write-Host $u.Description -ForegroundColor Gray
    Write-Host "Account Notes: " -ForegroundColor Green
    Write-Host $u.Info -ForegroundColor Gray
  }
}
else
{
  Write-Host "User $fullname does not exist in AD." -ForegroundColor Red 
}
July 7th, 2015 3:01pm

clayman2,

Thanks so much!  That worked like a champ.


Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 3:35pm

You're welcome, glad I could be of assistance.
July 7th, 2015 4:04pm

clayman2,

Thanks so much!  That worked like a champ.


Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 7:31pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics