Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

get samAccount name of users with same last name

$
0
0

$users = Get-Content "C:\Temp\tmp\getSAM.txt"

foreach ($user in $users) {

    $sam = Get-ADUser -ldapfilter "(displayname=$user)" -Property displayname, samaccountname | Select-Object -Property displayname, samaccountname | Export-CSV "c:\temp\tmp\SAM.csv" -append

}

 

the above code works fine.   It reads the .txt file which has names in the following format.  Last name, first name.   My issue is this .txt file was generated from a different database, not active directory.  So many of the names are not entered the same as AD.  Example:

AD: BRION, MARK J.    other DB: Brion, Mark

As a result my script above does not get an samAccount name for this user, or for any user that does not have an exact match.    My question is how do I get the samAccount name based on just the users last name?   So if we had 3 users with a last name of XYZ I would get this:

XYZ, Samual   xyzsam1

XYZ, Tommy   xyztom1

XYZ, George   xyzgeo1

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles