Quick check for "AD User Exists"

There's a ton of examples of this kind of script, but for some reason each one I tried gave me an error. I'm not sure if it was due to powershell version changes or what, but this one should work on Powershell v4 and Windows Server 2012 r2.

# Takes a single column, line seperated lists of users, and simply tells you if they exist or not.
$UserCSV = get-content C:\Users\super-mpeterso\Desktop\userlist.csv
Foreach ($User in $UserCSV)
{
    # Try and Catch rather than an if statement so we don't get ugly red errors when a user isn't found.
    Try
    {
        $U = Get-aduser $User # The veriable holds the data, otherwise it is written to the terminal
        Write-Host "$user already exists"
    }
    Catch
    {
        Write-Warning "$user does not exist"
    }
}
BBK

BBK

Infosec analyst, lover of passwords, and owner of a giant goofy puppy.

Read More