Hi All i have userprincipalnames in a csv file in the below format.
usernames
user1@contoso.com
user2@contoso.com
i want to add these users to AD group from powershell. if there are any other user with user1.a@contoso.com or user2.a@contoso.com. i dont want those users to be added .i want to add only users which are in my excel sheet. For example manually if we try to add and search for user1 i will get a prompt for user1 and user1.a. this AD group provide licenses to an application and i am running out of licenses, so my ask is to add only users in my excel sheet. using the below syntax can i achieve it. please guide me.
Import-CSV C:\temp.csv |
Foreach-Object{
$FindSam = Get-ADUser -Filter "UserPrincipalName -eq '$($_.usernames)'"
Add-ADGroupMember -Identity "MYADGroup" -Members $FindSam -confirm:$false
else {
Write-Host "$($_.usernames) not found in AD as a UPN"
}
}