Link to home
Start Free TrialLog in
Avatar of Joe G
Joe GFlag for United States of America

asked on

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?
Avatar of arnold
arnold
Flag of United States of America image

There are many examples for vbscript or powershell to query AD, you can customize and use them to do what you want.
Avatar of Joe G

ASKER

I can't seem to find any... my search keywords are failing me.
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.
Avatar of Joe G

ASKER

I'm getting firstname lastname does not exist but it is pulling in the names from the text file.  Any suggestions?
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.
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.
Avatar of Joe G

ASKER

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.
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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joe G

ASKER

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?
Avatar of oBdA
oBdA

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

Avatar of Joe G

ASKER

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.
Avatar of Joe G

ASKER

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.
You only need to swap $FirstName and $LastName in line 5:
	$LastName, $FirstName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)

Open in new window

Avatar of Joe G

ASKER

awesome.  thank you!
Avatar of Joe G

ASKER

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?