locked
Powershell read-host RRS feed

  • Question

  • 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

    Thursday, June 1, 2017 2:41 PM

Answers

  • $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.

    • Marked as answer by Yorkie89 Wednesday, June 7, 2017 2:36 PM
    Thursday, June 1, 2017 7:21 PM

All replies

  • $fullname = Read-Host "enter a fullname"
    Get-ADUser -LDAPFilter "(name=$fullname)" | select distinguishedname
    Thursday, June 1, 2017 2:53 PM
  • $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.

    • Marked as answer by Yorkie89 Wednesday, June 7, 2017 2:36 PM
    Thursday, June 1, 2017 7:21 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="


    \_(ツ)_/

    Thursday, June 1, 2017 7:52 PM