Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 13, 2013 1:24 PM
I need a scipt to get samaccountname from an e-mail address.
I have the script for getting samaccountname from display name
$mbxlist = Get-Content E:\Sid\Users.txt
ForEach ($mbx in $mbxList ) {
Get-ADUser -Filter "Name -eq '$mbx'" | Select -Property SamAccountName
}
What changes i need to make in this script to use an e-mail address instead of display name
The text file Users.txt contains display names. do i need to replace the Contents of the text files with e-mail addresses?
Please advise
All replies (19)
Friday, December 13, 2013 1:37 PM ✅Answered
Simple change, as long as the text file contains nothing but email addresses
Get-Content E:\Sid\users.txt | Foreach-Object { Get-ADUser -Filter {EmailAddress -eq "$_"}| Select-Object -ExpandProperty SamAccountName}
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Friday, December 13, 2013 2:22 PM
I ran the above code. did not work
Do i need to change the contents of the users.txt file from display name to e-mail address?
Currently the users.txt file has 2 display names
Rao
Sid
Do i need to change it to e-mail address?
PLease advise
Friday, December 13, 2013 2:42 PM | 2 votes
Hi,
Display names don't generally work very well, but give this a try before rebuilding your input file:
Get-Content E:\Sid\users.txt | ForEach {
Get-ADUser -Filter "DisplayName -eq '$_'" | Select SamAccountName
}
If that doesn't work, try putting email addresses in your input file and then try this:
Get-Content E:\Sid\users.txt | ForEach {
Get-ADUser -Filter "mail -eq '$_'" | Select SamAccountName
}
Don't retire TechNet! - (Don't give up yet - 12,420+ strong and growing)
Friday, December 13, 2013 2:56 PM
Get-Content E:\Sid\users.txt | ForEach {
Get-ADUser -Filter "mail -eq '$_'" | Select SamAccountName
} Thx bro, it works
Friday, December 13, 2013 2:56 PM | 1 vote
Yes you will need to change from display name to emailaddress in the text file, as you stated you want to find sAMAccountName based on users email address
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
Friday, December 13, 2013 8:19 PM
I need a scipt to get samaccountname from an e-mail address.
I have the script for getting samaccountname from display name
$mbxlist = Get-Content E:\Sid\Users.txt
ForEach ($mbx in $mbxList ) {
Get-ADUser -Filter "Name -eq '$mbx'" | Select -Property SamAccountName
}What changes i need to make in this script to use an e-mail address instead of display name
The text file Users.txt contains display names. do i need to replace the Contents of the text files with e-mail addresses?
Please advise
Note that, unlike the sAMAccountName (and, ideally, the SMTP address) display names are not necessarily unique.
Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.
Monday, March 12, 2018 5:21 PM
I am not getting the proper output either in csv or txt format. It's only giving the last user sam account name.
Get-Content E:\Sid\users.txt | ForEach { Get-ADUser -Filter "mail -eq '$_'" | Select SamAccountName | export-csv -path C:\users\user1\desktop\abc.csv }
Nilabh Verma
Monday, March 12, 2018 8:52 PM
You are rewriting your abc.csv in each loop, thats why u get only the last account name.
Monday, May 21, 2018 2:59 PM
Richard, This is very helpful. I am trying to do the same only I need multiple fields returned besides SamAccountName. I need Office, Manager, etc. as well... Any help would be very much appreciated.
Get-Content C:\Users\eric\documents\UNIQUE.csv | ForEach {
Get-ADUser -Filter "mail -eq '$_'" | Select SamAccountName
}
export-csv -path C:\Users\eric\documents\output.csv -NoTypeInformation
Monday, May 21, 2018 3:49 PM
Boom! You rock! Thanks for the quick response.
Thursday, October 11, 2018 2:56 AM
Hi Richard:
Thank you for your contributions. I have been trying to solve the same problem, have run all suggestions in this thread, yet I still have issues.
Import-Csv C:\temp\FileName.csv | % {
Get-ADUser -Filter "mail -eq '$_'" | Export-Csv C:\temp\FileName_Processed.csv -NoTypeInformation
}
1. FileName.csv contains email addresses
2. Powershell runs this without any complaints
3. FileName_Processed.csv does not contain anything
My main goal is to add a list of folks to a Distribution Group. I, however, have not figured out how to do that other than using the SamAccountName. Hence, my above attempt to convert, then add to DG
Much appreciated...Nari
Thursday, October 11, 2018 4:58 AM
Get-ADUser -filter "mail -eq '$_'"
The line above is not contain any query on the object, u need to add your query by adding this
| Select SamaccountName
Get-Content .\filename.txt |
ForEach-Object{Get-ADUser -Filter "Mail -eq '$_'"} |
Select-Object SamAccountName
Thursday, October 18, 2018 1:01 AM
Thanks so much for your assistance, Kt. I am not certain, however, why I'm having such a tough time with this. I tried your example and also tried the following (aside from the aforementioned in my previous post):
Import-Csv c:\temp\filename.csv | % {
Get-ADUser -Filter "Mail -eq '$_'"} | select SamAccountName | Export-Csv c:\temp\filename_SAM.csv
Import-Csv c:\temp\filename.csv | % {
Get-ADUser -Filter "Mail -eq '$_'" | select SamAccountName } | Export-Csv c:\temp\filename_SAM.csv
Import-Csv c:\temp\filename.csv | ForEach-Object {Get-ADUser -Filter "Mail -eq '$_'" } | Select-Object SamAccountName | Export-Csv c:\temp\filename_SAM.csv
In each case:
1. powershell does not complain
2. it creates c:\temp\filename_SAM.csv
3. in all 3 instances, the output CSV file contains no data
Regards,
Nari
Thursday, October 18, 2018 1:15 AM
I strongly recommend taking the time to learn basic PowerShell.
Get-Content E:\Sid\Users.txt |
ForEach-Object{
Get-ADUser -Filter "Mail -eq '$_'"
} |
select SamAccountName
You need to research the CmdLets you are trying to use. That is why PS includes an extensive help system.
As long as you don't have a good basic understanding of PS you will be at the mercy of your badly asked questions and the lack of experience of many that will try to help you.
\(ツ)_/
Thursday, October 18, 2018 2:25 AM
If you're importing a CSV file then each column has a unique name. You haven't used any of them in the filter criterion.
Try replacing $_ with $_.Mail (assuming that 'Mail' is the name of the column in the CSV that contains the value you're using in the filter).
If the file is a plain text file that holds only the 'Mail' address then change your Import-CSV to Get-Content and leave the '$_' as it is.
Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
Thursday, October 18, 2018 2:31 AM
Where did this mysterious CSV come from. The original post clearly shows a text file.
Original request:
$mbxlist = Get-Content E:\Sid\Users.txt
ForEach ($mbx in $mbxList ) {
Get-ADUser -Filter "Name -eq '$mbx'" | Select -Property SamAccountName
}
What changes i need to make in this script to use an e-mail address instead of display name
The simple answer would be:
$mbxlist = Get-Content E:\Sid\Users.txt
ForEach ($mbx in $mbxList ) {
** Get-ADUser -Filter "Mail -eq '$mbx'" | Select -Property SamAccountName**
}
**
So why do we need a CSV and why is this so difficult?
KL offered a simple update to this:
Get-Content .\filename.txt |
ForEach-Object{Get-ADUser -Filter "Mail -eq '$_'"} |
Select-Object SamAccountName**
Either will work. I reposted this and stressed the importance of understanding why.
If users want to learn then answerers need to be careful how they answer. If a user just wants someone to do it for them then they need to change profession to fast food worker or shoe salesman.
\(ツ)_/
Monday, October 22, 2018 9:24 PM
Thanks for the response.
Monday, October 22, 2018 9:29 PM
Hi Rich:
Thank you for bringing up column names. I was aware of referencing columns, however, for the purpose of pulling in the data from my CVS, it seems to make no difference as there is only one column. To be certain I confirmed this by running the commands line by line in the ISE and sure enough it pulled the e-mail addresses and printed them on the screen. Great point though. Your response is much appreciated. :)
Thursday, April 11, 2019 4:04 AM
Its few years late.. anyways!
I'd rather query from exchange modules to avoid searching all the users. This script will give the answer.
logfile = "c:\temp\samaccountname.txt"
Function LogWrite
{
param ([string]$logstring)
Add-content $Logfile -value $logstring
}
$users = Get-Content "C:\Userslist.txt"
foreach($user in $users)
{
$a =get-recipient -identity $user | select Samaccountname
$samaccountname = $a.Samaccountname
LogWrite $samaccountname
}
Mayank Sharma Support Engineer at Microsoft working in Enterprise Platform Support.