Help with powershell loop

am trying to create a powershell script to keep the new user in a specified OU. Example is below:

echo “Please provide the user’s department information”

echo “1 HR”; echo “2 OD”; echo “3 ELT”

$OUmove = Read-Host “Enter a number from the list”

if ($OUmove -eq 1) {

$var = “ou=users,ou=HR,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq 2) {

$var = “ou=users,ou=OD,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq 3) {

$var = “ou=users,ou=ELT,ou=departments,dc=dc,dc=local”}

else {echo “Please provide a number from 1-3 only”}

I do not want this script to break if someone use the input other 1 or 2 or 3. For instance, if you put 4 it should go back and starts asking you the number from the list, can someone help me so I can to create a loop?

Thanks in advance.

Navi,
Welcome to the forum. :wave:t4:

Before we proceed … When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

For this case you could use a while loop …

or … a little more sophisticated a choice prompt

https://4sysops.com/archives/read-host-and-the-choicedescription-class-prompt-for-user-input-in-powershell/

Regardless of all that - it is considered bad style to use aliasses in scripts. You should avoid them. You may read more about best practices and styles for PowerShell in the

1 Like

I create a while loop here but it is just spitting "please enter a valid number, can you please help me what I can do next?

$oumove = read-host “Please enter the user’s department:”

do {

if ($OUmove -eq ‘1’) {
$var = “ou=users,ou=HR,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘2’) {
$var = “ou=users,ou=OD,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘3’) {
$var = “ou=users,ou=ELT,ou=departments,dc=dc,dc=local”}

else{echo “Please enter a valid number”}

}until("")

Write-host “The ou is $var”

You had to intentionally ignore/overlook the fair request to have you format your code as code. Please read the guide if you need more details on how to format it properly. It really is as easy as copying your code into your clipboard, position your cursor where you want the code, click the preformatted button </> and immediately paste your code. Please edit your two posts accordingly. Thank you!

I urgently encourage you to read the help you’ve got recommended.

No. You did not. I’m actually curious about how you came from my recommendation to use a while loop to the code you posted. :smirk:

So you decided to use do loop. :smirk: So why didn’t you put a condition? :thinking:

In case you don’t know how to start you should simply copy the examples from the help and modify it step by step until it fits your needs.

You may start with this:

do {
    [int]$ReadHostVariable = Read-Host -Prompt 'Enter a number between 0 and 4'
} 
until ($ReadHostVariable -gt 0 -and $ReadHostVariable -lt 4)
'You entered {0}' -f $ReadHostVariable

Since the output of Read-Host is a string by default you will get an error if someone enters something else than an integer. In this case the type cast to [int] will not work. If you want to catch this you will have to implement error handling or you switch to my other recommendation. :wink:

Hello Olaf, thank you so much, how would go forward in this code, if a person select a number from 1-3 and it should show the current value of $var and ask for “y or n” to confirm it? do I need to execute a different code after this or it can be within this?

echo "1.HR"; echo "2.ELT"; echo "3.ELT"

do {
    [int]$oumove = Read-Host -Prompt 'Which department does this user belongs to?'

    if ($OUmove -eq ‘1’) {
$var = “ou=users,ou=HR,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘2’) {
$var = “ou=users,ou=OD,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘3’) {
$var = “ou=users,ou=ELT,ou=departments,dc=dc,dc=local”}

else{echo “Please enter a valid number, it should from 1-3”}

} 
until ($oumove -gt 0 -and $oumove -lt 4)

I wouldn’t. :smirk: … I’d prefer using a choice prompt.

BTW: You used “ELT” twice!! :point_up_2:t4: :wink:

You wrap everything in another loop and ask for “yes” or “no”.

Again: You should not use aliasses in your scripts. For example - look at this:

@'
1.HR
2.OD
3.ELT
'@

That’s easier to read and easier to maintain. Consider using VSCode for your PowerShell development. It will help you avoiding a lot of style and best practice failures.

Using the code, can you please help me out how I can wrap it, so I will ask for ‘yes’ or ‘no’ and if someone enter any other entry it should roll back and ask you to select the department again?

@'
1.HR
2.OD
3.ELT
'@

do {
    [int]$oumove = Read-Host -Prompt 'Which department does this user belongs to?'

    if ($OUmove -eq ‘1’) {
$var = “ou=users,ou=HR,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘2’) {
$var = “ou=users,ou=OD,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘3’) {
$var = “ou=users,ou=ELT,ou=departments,dc=dc,dc=local”}

else{echo “Please enter a valid number, it should from 1-3”}

} 
until ($oumove -gt 0 -and $oumove -lt 4)

Wow. Now you’re beeing lazy. Why don’t you try it yourself first. If you have problems you come back with your problems and we’ll try to help. But we will not write your code for you.

I tried to wrap the content in do loop again, however it is saying missing block statement in the last else

@'
1.HR
2.OD
3.ELT
'@

do
    {$confirm = Read-Host "Is the user current department is $var (y/n)"
    if ($confirm -eq 'y','yes') {

do {
    $oumove = Read-Host -Prompt 'Which department does this user belongs to?'

    if ($OUmove -eq ‘1’) {
$var = “ou=users,ou=HR,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘2’) {
$var = “ou=users,ou=OD,ou=departments,dc=dc,dc=local”}

elseif($OUmove -eq ‘3’) {
$var = “ou=users,ou=ELT,ou=departments,dc=dc,dc=local”}

else{echo ""$oumove" is a not valid number, Hint: Select a number from 1-3"}

    } 
until ($oumove -gt 0 -and $oumove -lt 4)}
else($confirm = 'no','n'){
echo "Please enter the OU info again"}
until($confirm = 'yes', 'y')}