FN-GM Posted January 9, 2017 Posted January 9, 2017 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
Cache Posted January 9, 2017 Posted January 9, 2017 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 1
FN-GM Posted February 8, 2017 Author Posted February 8, 2017 Hello, I have just tried this but it doesn't seem to work Thanks
DJ-1701 Posted February 8, 2017 Posted February 8, 2017 (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 February 8, 2017 by DJ-1701 1
kennysarmy Posted February 8, 2017 Posted February 8, 2017 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)
FN-GM Posted February 8, 2017 Author Posted February 8, 2017 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
kennysarmy Posted February 8, 2017 Posted February 8, 2017 We have about 5,000 users in AD so not that easy Delete some then 1
FN-GM Posted February 8, 2017 Author Posted February 8, 2017 @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 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now