Home > Software design >  How to find AD user with firstname and lastname?
How to find AD user with firstname and lastname?

Time:08-24

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

CodePudding user response:

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)'" |...

CodePudding user response:

$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 
}
  • Related