Powershell Script to Add AD User Accounts to Group using UPNs in an input file

I currently use the below script to add users to a group by SAMAccountName.  I have a file that contains UPNs instead however, and this does not work.  How can I modify the script to work with UPNs instead of SAMAccountName?

$Users = Get-Content UserList.txt
ForEach ($User in $Users) 
{
Add-ADGroupMember -Identity 'GroupName' -Members $Users
}

Open in new window

LVL 1
fireguy1125Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MaheshArchitectCommented:
Try below
save your file as csv and Add "upn" as header
$Users = import-Csv UserList.csv
$users |  Get-ADUser -Filter (UserPrincipalName -eq $_.upn) | Add-ADGroupMember -Identity 'GroupName' -Members $_

Open in new window

0
Jason CrawfordTransport NinjaCommented:
Try this:

foreach ($user in (Get-Content .\userlist.txt)) {
    $upn = Get-ADUser $user
    Add-ADGroupMember 'GroupName' -Members $upn
}

Open in new window

0
fireguy1125Author Commented:
Neither of the above are working for me.

For Mahesh's script, I get this error:

UserPrincipalName : The term 'UserPrincipalName' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At line:1 char:31
+ $users |  Get-ADUser -Filter (UserPrincipalName -eq $_.upn) | Add-ADG ...
+                               ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (UserPrincipalName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Open in new window


For Jason's script, I get error:

Get-ADUser : Cannot find an object with identity: 'TestUser1@domainname.com' under: 'DC=domainname,DC=com'.
At line:2 char:12
+     $upn = Get-ADUser $user
+            ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (TestUser1@domainname.com:ADUser) [Get-ADUser], ADIdentityNotFound
   Exception
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
   icrosoft.ActiveDirectory.Management.Commands.GetADUser

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:3 char:39
+     Add-ADGroupMember 'GroupName' -Members $upn
+                                       ~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

Get-ADUser : Cannot find an object with identity: 'TestUser2@domainname.com' under: 'DC=domainname,DC=com'.
At line:2 char:12
+     $upn = Get-ADUser $user
+            ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (TestUser2@domainname.com:ADUser) [Get-ADUser], ADIdentityNotFound
   Exception
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
   icrosoft.ActiveDirectory.Management.Commands.GetADUser

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:3 char:39
+     Add-ADGroupMember 'GroupName' -Members $upn
+                                       ~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

Open in new window


Thanks.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jason CrawfordTransport NinjaCommented:
My fault, try this instead:

foreach ($user in (Get-Content .\userlist.txt)) {
    $upn = Get-ADUser -Filter {UserPrincipalName -eq $user}
    Add-ADGroupMember 'GroupName' -Members $upn.samaccountname
}

Open in new window

0
fireguy1125Author Commented:
I get this error now:

Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:3 char:39
+     Add-ADGroupMember 'GroupName' -Members $upn.samaccountname
+                                       ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou
   pMember

Open in new window

0
Jason CrawfordTransport NinjaCommented:
You have to replace 'GroupName' with the name of the AD group you're adding these members to.

Untitled.png
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fireguy1125Author Commented:
Thanks Jason,  Indeed I replaced the group name with what I was using, and actually it did add the members to the group successfully.  I noticed my input file still had the UPN heading, which is what was throwing the error.  I removed it, and just kept the UPNs, and it ran successfully without throwing the errors.  Thanks again!
0
Jason CrawfordTransport NinjaCommented:
Glad I could help and sorry for the initial confusion.  Take care :)
0
MaheshArchitectCommented:
@fireguy:
Can you please recheck if script is working for you as I have checked all combinations and its giving error with / without headers or with .txt file as well.

The error is either its not able to accept -members value or its not able to get UserPrincipalName at 1st place in query

If its working for you, please post last successful working code
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.