Jump to content

Recommended Posts

Posted

Hello,

 

I have a list of usernames. I want to check if they already exist in AD. Does anyone know how to do this in Powershell please? All the stuff I have found involves creating the user account after the check. I just want to check only.

 

Can anyone help please?

 

Thanks

Posted

This is an extract from my user creation script if it helps:

 

###############################################################################

##

##	Required CSV Format and Headings and Data Example (Data Example in Brackets)

##	(A) FirstName 
##	(B) LastName 
##	(C) SamAccountName 
##	(D) Password 
##	(E) IntakeYear 

Start-Transcript -path E:\intake2015add5.txt -append

Import-Module ActiveDirectory

# Import the CSV and setup additional variables

Import-Csv .\intake2015add5.csv | foreach-object {

#Variable Attributes created from file

$name = $_.SamAccountName

$usercheck = $null

# check if username exists already in AD

$usercheck = Get-ADUser -Filter {sAMAccountName -eq $name}

# Check search result to see if it's Null and then create the user

If ($usercheck -eq $null) {

Write-Host "User Check Completed - " + $_.SamAccountName + " doesn't exist" `n`r

}

# If it's not null, print out to screen

Else { Write-Host "User: "$_.SamAccountName "already exists!"`n`r}

}

Stop-Transcript

  • Thanks 1
  • 5 weeks later...
Posted (edited)

@FN-GM Copied some coding I made for our user creation and amended it for the purpose of checking if a Username exists for ya. :)

 

# Access Active Directory PowerShell Commands.
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
# Import List Of Accounts From CSV.
$ListOfAccounts=IMPORT-CSV C:\test.csv -Header ("Username")
FOREACH ($Account in $ListOfAccounts)
{
   
   # If the account exists, inform, if it does not exist also inform.
   $Username = $Account.Username
   If ((Get-ADUser -Filter {SamAccountName -eq $Username}) -eq $Null)
   {
       Write-Host "I am sorry, $Username does not exist."
   }
   Else
   {
       Write-Host "$Username already exists."
   }
}

 

Edit: With this you just need a file with a list of usernames for it to check. :)

Edited by DJ-1701
  • Thanks 1
Posted
Hello,

 

I have a list of usernames. I want to check if they already exist in AD. Does anyone know how to do this in Powershell please? All the stuff I have found involves creating the user account after the check. I just want to check only.

 

Can anyone help please?

 

Thanks

 

I've got a saved query in my AD that lists "All Users" - so it's easy to list in alphabetical order and see if an account exists...

 

The query string is simply:

 

(&(|(&(objectCategory=person)(objectSid=*)(!samAccountType:1.2.840.113556.1.4.804:=3))(&(objectCategory=person)(!objectSid=*))(&(objectCategory=group)

Posted
I've got a saved query in my AD that lists "All Users" - so it's easy to list in alphabetical order and see if an account exists...

 

The query string is simply:

 

(&(|(&(objectCategory=person)(objectSid=*)(!samAccountType:1.2.840.113556.1.4.804:=3))(&(objectCategory=person)(!objectSid=*))(&(objectCategory=group)

 

We have about 5,000 users in AD so not that easy :)

Posted
@FN-GM Copied some coding I made for our user creation and amended it for the purpose of checking if a Username exists for ya. :)

 

# Access Active Directory PowerShell Commands.
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
# Import List Of Accounts From CSV.
$ListOfAccounts=IMPORT-CSV C:\test.csv -Header ("Username")
FOREACH ($Account in $ListOfAccounts)
{
   
   # If the account exists, inform, if it does not exist also inform.
   $Username = $Account.Username
   If ((Get-ADUser -Filter {SamAccountName -eq $Username}) -eq $Null)
   {
       Write-Host "I am sorry, $Username does not exist."
   }
   Else
   {
       Write-Host "$Username already exists."
   }
}

 

Edit: With this you just need a file with a list of usernames for it to check. :)

 

It works!! Thanks :)

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...