I don't really see the nested loop here. Your code is just prompting for a department which is independent of anything else. After that would just be an if
statement asking whether you should add the user or not.
# Get department
do
{
} until (...)
# Verify user
$choice = Read-Host -Prompt "Do you want to add the user (y/n)? "
if ($choice -eq "y") {
# Do your work here
}
Of course if you're doing this for a set of users then a loop makes more sense.
$users = @('Bob', 'Sue')
foreach ($user in $users) {
$choice = Read-Host -Prompt "Do you want to add the user (y/n)? "
if ($choice -ne "y") {
continue
do
{
} until (...)
}
But ideally you'd wrap that choice logic into a function so it is easier to read. In fact Powershell already has PromptForchoice that can do this albeit with less than ideal code. Alternatively you could roll your own. Irrelevant you can narrow down the code and make it easier to read if you did.
$departments = @('HR', 'OD', 'ELT')
foreach ($user in $users) {
if (ShouldAddUser $user) {
$dept = PromptForDepartment($departments)
$ou = "ou=users,ou=$dept,ou=departments,dc=dc,dc=local"
# Do your work
}
}