1

I'm trying to make a check in Powershell where I have a list of AD users inside a variable and then check if each one of those usernames exist in AD. If they do it will add those to another variable and if it doesn't it will output those to stdout that it's not found.

For example the input variable would be something like:

$CHECKUSERS = bakerc fosterv englesp

And if bakerc and englesp exist in AD using Get-ADUser (or a different command if there is a better option) it would then add them to another variable like:

$VERIFIEDUSERS = bakerc englesp

It then outputs fosterv to stdout:

fosterv not found

1 Answer 1

1

Not a fan of writing your code for you but it is a simple enough request.

Not the most efficient way to do it because of multiple AD calls, but it does the job.

$CHECKUSERS = "bakerc","fosterv","englesp"
$VERIFIEDUSERS = @()
$INVALIDUSERS = @()

Foreach($user in $CHECKUSERS) {
    Try {
        Get-ADUser $user | Out-Null
        $VERIFIEDUSERS += $User
    } Catch {
        $INVALIDUSERS += $user
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.