ak2595
ak2595

Reputation: 321

How to find AD user with firstname and lastname?

How can i find users samaccountname with firstname and lastname?

$users =
@'
LastName;FirstName;
Roy;Adam
Smith;George
'@ | ConvertFrom-Csv -Delimiter ";"

ForEach ($User in $Users) {
  Get-ADUser -Filter {GivenName -like $user.firstname -and Surname -like $user.lastname} | Select-Object -ExpandProperty sAMaccountName 
}

im getting an error

+ CategoryInfo          : InvalidArgument : (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Upvotes: 1

Views: 4653

Answers (2)

Narayana Lvsl
Narayana Lvsl

Reputation: 373

$users =
@'
LastName;FirstName;
Roy;Adam
Smith;George
'@ | ConvertFrom-Csv -Delimiter ";"    
ForEach ($User in $Users) {
  Get-ADUser -Filter {GivenName -match $user.firstname -and Surname -match $user.lastname} | Select-Object -ExpandProperty sAMaccountName 
}

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174435

The property access operation inside the {...} filter block won't resolve correctly.

Use a string-based filter instead:

Get-ADUser -Filter "GivenName -like '$($user.firstname)' -and Surname -like '$($user.lastname)'" |...

Upvotes: 4

Related Questions