Reputation: 3
I am tryign to add employee numbers to all of my AD users by importing a csv file with first name, last name, employee number and email fields.
The issue I am having is that I have come across some users that the first name in the csv file does not match up to the first name in AD.
I have searched for weeks on trying to figure out how to get powershell to search ad using the last name and only the first letter of the first name field from the csv, but I juat cannot figure it out.
Any help would be greatly appreciated.
Here is my script:
Import-Module ActiveDirectory
ForEach ($User in (Import-CSV -Path 'C:\Users\mike.mullen\Desktop\Active Users with Email.csv'))
{
$Last = $User.LastName
$First = $User.FirstName
$EmployeeNumber = $User.EmployeeId
Get-ADUser -Filter '(Surname -eq $Last) -and (GivenName -like $First)' |
Set-ADObject -Replace @{EmployeeNumber="$EmployeeNumber"}
}
Upvotes: 0
Views: 368
Reputation: 174690
If you want to construct a filter that searches for first names with a specific prefix (ie. the first letter from the first name from the CSV), you'll need to:
$First
*
character to it:The first step can be accomplished with the String.Substring()
method
$Surname = $User.LastName
$FirstLetter = $User.FirstName.Substring(0,1) + '*'
Get-ADUser -Filter '(Surname -eq $Surname) -and (GivenName -like $FirstLetter)'
Upvotes: 1