# Search AD for for a user with a specified attribute.

###
#
# Search AD for a user with specific attributes.
#
# Commoon properties that you may search on.
# -OfficePhone
# -MobilePhone
# -DisplayName
# -GivenName
# -Surname
# -Title
# -Department
# -Fax
# -HomeDirectory
# -HomeDrive
# -EmailAddress
# -Group Membership
#
###

function Show-Menu
{
    param (
        [string]$SearchAD="Search Active Directory"
        )
        cls
        Write-Host "================ $SearchAD ================"

            Write-Host "0: Press 0 for OfficePhone"
            Write-Host "1: Press 1 for MobilePhone"
            Write-Host "2: Press 2 for DisplayName"
            Write-Host "3: Press 3 for GivenName"
            Write-Host "4: Press 4 for Surname"
            Write-Host "5: Press 5 for Title"
            Write-Host "6: Press 6 for Department"
            Write-Host "7: Press 7 for Fax"
            Write-Host "8: Press 8 for HomeDirectory"
            Write-Host "9: Press 9 for HomeDrive"
            Write-Host "10: Press 10 for EmailAddress"
            Write-Host "Q: Press 'Q' to quit."
}

do 
{
    Show-Menu
    $Input = Read-Host "Please choose a search property"
    switch ($Input)
    {
        '0' {
            cls
            $SearchTerm = Read-Host 'Please enter the OfficePhone to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.OfficePhone -like $SearchTerm }
            }
        '1' {
            cls
            $SearchTerm = Read-Host 'Please enter the MobilePhone to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.MobilePhone -like $SearchTerm }
            }
        '2' {
            cls
            $SearchTerm = Read-Host 'Please enter the DisplayName to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.DisplayName -like $SearchTerm }
            }
        '3' {
            cls
            $SearchTerm = Read-Host 'Please enter the GivenName to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.GivenName -like $SearchTerm }
            }
        '4' {
            cls
            $SearchTerm = Read-Host 'Please enter the Surname to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.Surname -like $SearchTerm }
            }
        '5' {
            cls
            $SearchTerm = Read-Host 'Please enter the Title to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.Title -like $SearchTerm }
            }
        '6' {
            cls
            $SearchTerm = Read-Host 'Please enter the Department to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.Department -like $SearchTerm }
            }
        '7' {
            cls
            $SearchTerm = Read-Host 'Please enter the Fax to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.Fax -like $SearchTerm }
            }
        '8' {
            cls
            $SearchTerm = Read-Host 'Please enter the HomeDirectory to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.HomeDirectory -like $SearchTerm }
            }
        '9' {
            cls
            $SearchTerm = Read-Host 'Please enter the HomeDrive to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.HomeDrive -like $SearchTerm }
            }
        '10' {
            cls
            $SearchTerm = Read-Host 'Please enter the EmailAddress to search on: '
            get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.EmailAddress -like $SearchTerm }
            }
        'q' {
                 return
            }
    }
    pause
}
until ($input -eq 'q')

#$SearchTerm = Read-Host 'Please enter term you want searched on: '


#get-aduser -Filter * -SearchBase "ou=<OU_Name>,ou=<OU_Name>,dc=<Domain>,dc=com" -Properties * | select SamAccountName,OfficePhone,MobilePhone,GivenName,Surname,DisplayName,Title,Department,Company,Fax,HomeDirectory,HomeDrive,EmailAddress | where {$_.Surname -like $SearchTerm }



[System.GC]::Collect()