How to check if an AD user exists

46,518

Solution 1

Here's one quick way:

([ADSISearcher] "(sAMAccountName=kendyer)").FindOne()

If it returns no results, the user account was not found.

As a function:

function Test-ADUser {
  param(
    [Parameter(Mandatory = $true)]
    [String] $sAMAccountName
  )
  $null -ne ([ADSISearcher] "(sAMAccountName=$sAMAccountName)").FindOne()
}

Solution 2

The problem with Get-ADUser -Identity $Username.Text is that it throws an exception when it fails to find something. If you want to avoid that, you have to search with a filter:

if (!(Get-ADUser -Filter "sAMAccountName -eq '$($Username.Text)'")) {
    Write-Host "User does not exist."
}

Otherwise, you can do something like:

try {
    Get-ADUser -Identity $Username.Text
    $UserExists = $true
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityResolutionException] {
    Write-Host "User does not exist."
    $UserExists = $false
}

Solution 3

I use this function in many of my scripts. If you just run Test-ADUser -Username $Username, it will return the user properties AND true if the user exists and False if it does not.

If using to test a condition (does the user exist?) it will return true or false

Save the function and the export Export-ModuleMember as a .psm1 file. Example: ADutils.psm1

Create a folder with the same name as the file. Example: ADutils

Put the file in the folder

Put the folder in C:\Windows\System32\WindowsPowerShell\v1.0\Modules\

Restart power shell and import-module ADutils -verbose

(Pick a good name for the file and folder. This will be the module name. You can add a great many more functions to this module by writing the function and ensuring that the Export-ModuleMember -Function exists for every function you write)

Function Test-ADUser {  
   [CmdletBinding()]  
  param(  
    [parameter(Mandatory=$true,position=0)]  
    [string]$Username  
    )  
     Try {  
       Get-ADuser $Username -ErrorAction Stop  
       return $true  
       }   
    Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {  
        return $false  
        }  
}   
Export-ModuleMember -Function Test-ADUser  


IF (Test-ADUser -Username w096224){  
   (New-Object -ComObject Wscript.Shell).PopUp("This username already exists.    Please choose another")  
}
Share:
46,518
cnelson
Author by

cnelson

Updated on July 09, 2021

Comments

  • cnelson
    cnelson almost 3 years

    I am working on error checking in my "Copy AD User" powershell script. I use forms to ask for specific information, and the purpose of this question is to make sure i'm putting in error checking correctly.

    IF ($Username.Text -eq Get-ADUser ($Username.Text))
        {$wshell = New-Object -ComObject Wscript.Shell
        $wshell.PopUp("This username already exists.  Please choose another")}
    

    The $Username.Text is the text box where the username for the new account is being pulled from. I want to run this through AD to see if that username already exists, and then display a message if it does.

    Am I going about it the correct way?

    Pastebin of Full Code

  • Bill_Stewart
    Bill_Stewart over 6 years
    This answer is redundant.