Function to retrieve Ad user account details using read-host

by someguy at 2013-01-02 15:49:55

It seems that for some reason the variables are not retaining the data from the host and im a little stuck as to why.

Function Getuserdetails ([string]$givenname, [string]$Surname)

$Givenname = Read-Host ’ Please enter Firstname ’

$Surname = Read-Host ’ Please enter Lastname ’

Get-ADUser -properties * -Filter {(givenname -eq "$Givenname") -and (surname -eq "$Surname")} | Select-Object telephonenumber,office

by Klaas at 2013-01-03 00:11:16
In the function declaration you specify $givenname and $surname as input parameters. That means you want them to be supplied with the function call:
Getuserdetails John Smith
But then in the function body you want to fill those variables again?

Maybe you can make things more clear by showing a bit more of your code.

And a little optimization: it’s better to use -properties telephonenumber,office instead of * if you only want to return those 2 properties.
by RichardSiddaway at 2013-01-03 10:32:01
Please don’t do this

You have a function that has the names as parameters but then you use Read-Host to get the names?

if you are running the function from the command line make the parameters mandatory and then you will prompted for them if you forget. If this function is part of a bigger script you should already have the names.

I would recommend changing your function to this
function Getuserdetails {
param (


Get-ADUser -properties telephonenumber,office -Filter {(GivenName -eq $Givenname) -and (Surname -eq $Surname)}


The variables don't need to be in quotes - you would have been getting the object rather then the value
by someguy at 2013-01-03 12:51:17
Thank you guys for responding i will take your advice.

Thanks again.