Joe G
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?
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?
There are many examples for vbscript or powershell to query AD, you can customize and use them to do what you want.
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...
Will.
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 "
}
}
Will.
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.
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.
Will.
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 "
}
}
Will.
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.
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.
Will.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
Sorry, that csv example is a mess.
You first start your script, then pipe the output to Export-Csv:
You first start your script, then pipe the output to Export-Csv:
.\WhatEver.ps1 | Export-Csv -NoTypeInformation -Path C:\Temp\export.csv
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.
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)
ASKER
awesome. thank you!
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?
ASKER
new question link. I need to grab the email address now on the perfect match column.
https://www.experts-exchange.com/questions/28697243/How-do-I-find-and-match-AD-accounts-with-the-existing-script-to-include-their-emails-from-AD.html
https://www.experts-exchange.com/questions/28697243/How-do-I-find-and-match-AD-accounts-with-the-existing-script-to-include-their-emails-from-AD.html