# Function to retrieve Ad user account details using read-host

**URL:** <https://forums.powershell.org/t/function-to-retrieve-ad-user-account-details-using-read-host/1300>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/function-to-retrieve-ad-user-account-details-using-read-host/1300 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/function-to-retrieve-ad-user-account-details-using-read-host/1300/1 "2011-12-31T18:30:00Z")

</div>

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 {[CmdletBinding()]param ([parameter(Mandatory=$true)][string]$Givenname, [parameter(Mandatory=$true)][string]$Surname) 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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:04pm UTC](https://forums.powershell.org/t/function-to-retrieve-ad-user-account-details-using-read-host/1300/2 "2024-05-16T21:04:30Z")

</div>


