Get-ADUser - finding a user using both GivenName (FirstName) and Surname

Really simple..

Looking to create a powershell script that returns an AD result if a user is found.

I'm using FirstName and Surname as variables. My script so far looks a little something like this...

$FirstName = Read-Host "Please enter the First Name of the new user"
$Surname = Read-Host "Please enter the Surname of the new user"
Get-ADUser -f "GivenName -eq '$FirstName'" -and "Surname -eq '$Surname'"

When I run each part of the Get-ADUser command (so search by first name, search by surname) it returns the expected result.

When I add -and as an operator, so both conditions are satisfied, I get this:

Get-ADUser : A parameter cannot be found that matches parameter name 'and'.
At line:4 char:44
+ Get-ADUser -f "GivenName -eq '$FirstName'" -and "Surname -eq '$Surnam ...
+ ~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management. Commands.GetADUser

Where in the hell am I going wrong!?

1

1 Answer

I tried this and worked,

$FirstName = Read-host 'enter first name'
$LastName = Read-Host 'enter last name'
Get-ADUser -Filter "GivenName -eq '$FirstName' -and SurName -eq
'$LastName'"

check for the quotes on your code

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like