Check for existing user in active directory

This powershell script below allows me to take
an input file that looks like this (input-users.csv)

SamAccountName
ksk34
xyz444
jsmith
bjones

and make an output file that will look like this (out.csv)

ksk34,yes
xyz444,no
jsmith,no
bjones,yes

The client I was working at did NOT have any windows 2008 dcs, so i could not use the command get-aduser, instead I installed the quest tools and I am using get-qaduser

Basically, this script give me a yes or no to the question “does the user (samaccount) exist?” Special thanks to Raymond for helping me with the output (I’m still new at powershell)

$users = Import-Csv c:\scripts\input-users.csv
foreach ($user in $users)
{
 if (get-qaduser -SamAccountName $user.SamAccountName)
 {
 #Found...
 "$($user.SamAccountName),yes" | out-file out.csv -append
 }
 else
 {
 #NOT Found...
 "$($user.SamAccountName),no"| out-file out.csv -append
 }
}

========AND ANOTHER VERSION OF THIS LOOKS LIKE THIS====

$users = Import-Csv c:\temp\ALLSMTP1.csv
 foreach ($user in $users)
 {
 if (get-qaduser -email $user.SMTPaddress)
 {
 #Found...
 "$($user.SMTPaddress),yes" | out-file smtp-yes.csv -append
 }
 else
 {
 #NOT Found...
 "$($user.SMTPaddress),$($user.samaccountname),no"| out-file smtp-no.csv -append
 }
 }
Advertisements

About Mike

owner of blog
This entry was posted in Active Directory, PowerShell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s