fireguy1125
asked on
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
}
Try this:
foreach ($user in (Get-Content .\userlist.txt)) {
$upn = Get-ADUser $user
Add-ADGroupMember 'GroupName' -Members $upn
}
ASKER
Neither of the above are working for me.
For Mahesh's script, I get this error:
For Jason's script, I get error:
Thanks.
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
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
Thanks.
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
}
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Glad I could help and sorry for the initial confusion. Take care :)
@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
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
save your file as csv and Add "upn" as header
Open in new window