Add exact upns

Glenn Maxwell 11,496 Reputation points
2024-04-30T18:33:23.3266667+00:00

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"
         }
     }
Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,799 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,526 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,654 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,555 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,602 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,816 Reputation points
    2024-04-30T18:54:19.2+00:00

    No, that won't work . . . but it's close. Try this:

    Import-Csv C:\temp.csv |
        ForEach-Object {
            $FindSam = Get-ADUser -Filter "UserPrincipalName -eq '$($_.usernames)'" -ErrorAction SilentlyContinue
            if ($FindSam) {
                Add-ADGroupMember -Identity "MYADGroup" -Members $FindSam -Confirm:$false
            }
            else {
                Write-Host "$($_.usernames) not found in AD as a UPN"
            }
        }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Glenn Maxwell 11,496 Reputation points
    2024-05-02T12:26:00.17+00:00

    i am getting the below error.

    Get-ADUser : The search filter cannot be recognized
    
    At line:3 char:20
    
    + ...  $FindSam = Get-ADUser -Filter "UserPrincipalName -eq '$($_.usernames ...
    
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
    
        + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.