Share via


Powershell read-host

Question

Thursday, June 1, 2017 2:41 PM

Hi,

I am fairly new to powershell and have ran into an issue. I am wanting to search the AD for a user that is defined with read-host.

$user = read-host "Enter your name" | get-aduser -f {name -like "$user"} -searchbase "OU=,OU=,OU=,OU=,DC=,DC="

Can anyone tell where I am going wrong?

Thank You

All replies (3)

Thursday, June 1, 2017 7:21 PM ✅Answered | 1 vote

$user is a variable so you cant pipe "|" the value into Get-ADUser. You first need to store the value in your $user variable.

You are on the right track with $user = read-host "Enter your name" Your script should then start a new line with:
Get-ADUser -filter 'displayname -like "*$user*"' | select displayname, samaccountname

I would use displayname as the filter criteria as it is usually a combination of the first name and surname. That way you could enter Joe B and return Joe Blogs and Joe Blow from AD if you have users with similar name and surname combinations.


Thursday, June 1, 2017 2:53 PM

$fullname = Read-Host "enter a fullname"
Get-ADUser -LDAPFilter "(name=$fullname)" | select distinguishedname


Thursday, June 1, 2017 7:52 PM

"Name" is the CN name and not the fullname, DisplayNAme or SamAccountName.

$user = read-host "Enter your name login name"
get-aduser -Filter "SamAccountName -eq '$user'" -searchbase "OU=,OU=,OU=,OU=,DC=,DC="

\(ツ)_/