I have multiple display name of users in a csv file what should I do if I want to find their samaccount name and email address and from ad using PowerShell. and also a outcome if PowerShell cant find the user. we can do it manually but I can’t find any reliable script where it has been done in PowerShell. Please help me with it. Thanks
We have something similar to what you are requesting on Module 2: Active Directory & Identity with Windows Server under the section How to Administrate Active Directory with Windows PowerShell: Listing AD Users with PowerShell
Here you can see how to query for users looking for their samAccountName plus adding the email.
If you have a CSV file we need to import it from the PowerShell script and look in AD but it will depend on the values it contains.
Ricardo
$csv = Import-Csv “C:\Users\GjhjG\Desktop\Bo5.csv”
Foreach ($line in $csv)
{
get-aduser -filter {(GivenName -eq ‘$Users.FirstName’) -And (surname -eq ’ $Users.Lastname’)} | select Name, samaccountname
}
This is the code I have trying to run csv where I have the user first and last name but it is not running. Please take a look at it.
and I only have the first and last name and The code I made is not working
Can you post at least the first row of the CSV to give it a look? You can edit it if confifential.
Something that can work for you is this one I created.
# Import the AD Module
Import-Module ActiveDirectory
# Import Users
$csv = Import-Csv -Path “C:\Users\Administrator\Desktop\Bo5.csv”
Foreach ($user in $csv)
{
$fname = $user.FirstName
$lname = $user.LastName
get-aduser -filter {GivenName -eq $fname -and Surname -eq $lname'} -Properties * | select SamAccountName
}
Ricardo