Find a users manager

Hi all,

I am just starting to learn PowerShell.

I’ve been looking around and found many posts from users who had just got started in PS as well. I have been given this task

I have successfully completed the script but now want to add some validation.

Things I am trying to do:

  1. When a user inserts an input (Read-Host “Username?”), if there is an error of user not found, I want them to be met with another read host prompt “user not found, re-insert the username” and if the correct username is the inserted then the users name, title and manager are displayed, if again the username is incorrect then it would keep looping to the “user not found, re-insert username”

#keeps prompting for input if no input is inserted
Do{ $username = Read-Host “What is the username?”} until ($username)
usrDetails = get-ADUser $username -properties *|Select Name, Title, @(n=‘Manager’, e={(get-aduser $_.manager) name)}}

#I then trap error if the username is not found
Trap{“user not found” continue;}

I do another, DO/UNTIL loop similar to the previous, but this time with the Read-Host message “re-insert username”

However this time I want to loop back to the “re-insert user name prompt” if the username inserted is again not found and this needs to be continous until the user does insert the correct input.

It has been suggested to me to insert error.count and if > 0 then to call error.clear(), but it has also been explained to me as a bit of a cheat, to code it this way.

Can I get any suggestions on how I can do this the right way please?

Hi and welcome to the Powershell forum.
First thing’s first you should format your code using the </> code block formatting tool, sometimes found in the settings gear.
I’ve written something similar to what you’re doing so this is a quick modification and copy/paste of that.

Do {
    Try {
    $Answer = Read-Host -Prompt "What is the username?"
    $ADResult = Get-ADUser -Identity $Answer -Properties Title,Manager -ErrorAction Stop
    $Continue = $true
    } Catch {
        Write-Warning "User not found"
        $Continue = $false
} Until ($Continue)

$UserDetails = $ADResult | Select-Object -Property Name,Title, @{Name="Manager";Expression={(Get-ADUser -Identity $_.Manager).Name}}

I’m using the $Continue variable to signal whether or not we finish the Do loop. The first line that attempts the Get-ADUser specifies that the Error Action preference should be to “stop” which will send a terminating error that the Catch block will catch. This means that the line following where it sets $Continue to ‘true’ won’t happen if it fails. The catch block uses Write-Warning to display the message and then the Do loop starts again until you finally supply a valid username.

Note that in the -Properties parameter for Get-Aduser I’m specifying the additional non-default properties I want returned. This is significantly faster than using the wildcard * which returns all properties. I like to get in the habit of specifying the exact properties I want to avoid this.

Then for your $UserDetails variable the syntax was wrong on your Select-Object expression so I fixed that too.

Nice… Amazing! I see what you done there.

Thank you very much, have a nice day!