Scripted to find -samid from list of users out of Active Directory

I have an list of users (first name last name) and I need to find their samid in active directory.  Our naming style when creating a new AD account is usually the first letter of the first name and the last name e.g John Smith is jsmith.  Of course when there is a Jane Smith we would do something like jsmith1 or jsmith2.  

Is there a script to build a query to gather all possible samid with wild cards in a search from a list of users in excel or text file?
Joe GIT personalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
There are many examples for vbscript or powershell to query AD, you can customize and use them to do what you want.
0
Joe GIT personalAuthor Commented:
I can't seem to find any... my search keywords are failing me.
0
Will SzymkowskiSenior Solution ArchitectCommented:
Use the following command...
Add all of your users first name last name to a TXT file called c:\userstxt 1 on each line

Example
Mike Smith
Mark David
etc...
Import-module activedirectory
Get-Content "c:\users.txt" |
ForEach { 
if (Get-ADUser -Filter {displayName -like $_}) {
                 Write-host "$_ does exist"}
              else {
                   Write-host "$_ does not exist "
                   }
 } 

Open in new window


Will.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Joe GIT personalAuthor Commented:
I'm getting firstname lastname does not exist but it is pulling in the names from the text file.  Any suggestions?
0
arnoldCommented:
run dsquery user
https://technet.microsoft.com/en-us/library/cc725702.aspx
dsget to retrieve more info
https://technet.microsoft.com/en-us/library/cc732535.aspx
dsquery user | dsget user -name -samid -fn -ln

powershell AD cmdlets
https://technet.microsoft.com/en-us/library/ee617195.aspx

http://blogs.technet.com/b/heyscriptingguy/archive/2012/10/30/powertip-single-line-powershell-command-to-list-all-users-in-an-ou.aspx

dsquery OU
lists all your OUs.
This can be fed to
a powershell CMD in the blog link that will list members of each OU.
0
Will SzymkowskiSenior Solution ArchitectCommented:
Sorry,

When you are using the -Like you need to add a "*" at the end of all of the names in your text file. However you can use -eq instead but this means that the name in the txt file needs to be exactly the same as AD to ensure a match.

Import-module activedirectory
Get-Content "c:\users.txt" |
ForEach { 
if (Get-ADUser -Filter {displayName -eq $_}) {
                 Write-host "$_ does exist"}
              else {
                   Write-host "$_ does not exist "
                   }
 } 

Open in new window


Will.
0
Joe GIT personalAuthor Commented:
Will - I added a * to the end of a few names in the list for a test and the same thing came up but now with the * at the end.  Is there a switch cmdlet i'm missing?  or something else?  

the eq will not work for my purpose but I feel we are almost there with your powershell script.
0
Will SzymkowskiSenior Solution ArchitectCommented:
When I use the first script using the "*" at the end of the names it works completely fine for me. Just tested this in my lab.

Will.
0
oBdACommented:
Try it with this Powershell script; it generates the SamId from the first and last name; if it finds more than one match, it compares first and last names in AD with the ones from the list.
$UserFile = "C:\Temp\test.txt"
Import-Module ActiveDirectory
Get-Content -Path $UserFile | % {
	$Result = "" | Select-Object -Property FirstName, LastName, PerfectMatch, PossibleMatch
	$FirstName, $LastName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
	$Result.FirstName = $FirstName
	$Result.LastName = $LastName
	$SamId = $FirstName.SubString(0, 1) + $LastName
	If ($PossibleMatches = @(Get-ADUser -Filter "sAMAccountName -like '$($SamId)*'")) {
		$Result.PossibleMatch = ($PossibleMatches | Select-Object -ExpandProperty SamAccountName) -join ", "
		If ($PossibleMatches.Count -eq 1) {
			$Result.PerfectMatch = $PossibleMatches.SamAccountName
		} Else {
			If ($PerfectMatches = @($PossibleMatches | ? {($_.GivenName -eq $FirstName) -And ($_.Surname -eq $LastName)})) {
				If ($PerfectMatches.Count -eq 1) {
					$Result.PerfectMatch = $PerfectMatches.SamAccountName
				}
			}
		}
	}
	$Result | Write-Output
}

Open in new window

To generate a csv from the output:
.\WhatEver.ps1 -ExportCsv -NoTypeInformation -Path C:\Temp\export.csv

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joe GIT personalAuthor Commented:
obda - the first one worked perfectly.  However on the output csv I couldn't get it to work.  Do i create a separate instance or combine the csv output line with the first?
0
oBdACommented:
Sorry, that csv example is a mess.
You first start your script, then pipe the output to Export-Csv:
.\WhatEver.ps1 | Export-Csv -NoTypeInformation -Path C:\Temp\export.csv

Open in new window

0
Joe GIT personalAuthor Commented:
Awesome output file and it was exactly what I needed.  don't forget the pipe in between the powershell file and the output command for the 2nd output file comment.
0
Joe GIT personalAuthor Commented:
oBdA - what do I change in the script when I'm given a a last name first name report instead of first name first? Sorry, i tried to edit the script and reverse the lastname and firstname instances but that didnt' work.  Any help? please.
0
oBdACommented:
You only need to swap $FirstName and $LastName in line 5:
	$LastName, $FirstName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)

Open in new window

0
Joe GIT personalAuthor Commented:
awesome.  thank you!
0
Joe GIT personalAuthor Commented:
oBda - I'm going to open a new post but how would i gather the email address on only the perfect matches from the AD?
0
Joe GIT personalAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.