Question

Im trying to create a while loop that request the correct user input before the script continues.

What I wan't is to make while loop that runs if the value is NOT true. So if the user doens't exist the while loop ask for a new username but i can't figure out how to do this. Se the example down below that works if the user exists. I have tried -ne $true and -not i different ways without success.

while(Get-ADUser -Identity USERNAME)
{
Read-host "Please enter a existing user"
}
Was it helpful?

Solution

do
{
    $username = Read-host "Please enter an existing username"
} while ($username -eq "" -or (Get-ADUser -Filter {Name -eq $username}) -eq $null)

This is case insensitive though. This one might work better:

do
{
    $username = Read-host "Please enter an existing username"
} while ($username -eq "" -or (Get-ADUser -Filter "Name -like `"$username`"") -eq $null)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top