import-csv "C:\temp\testgroup.csv" | ForEach-Object {add-ADGroupMember -Identity $_.groupname -Members $_.username}

Hi Guys

I found this code and want to modify it to transfer users from one group to another using a .csv file.

so i made a .csv file and entered the information below. when i run the code i get a message

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:1 char:104

  • … bject {add-ADGroupMember -Identity $.groupname -Members $.username}
  • CategoryInfo : InvalidData: (:slight_smile: [Add-ADGroupMember], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

members,groupname
Frank Mendoza,VDI_production

4 Spice ups

What does your CSV file look like? Does it have a ‘groupname’ and ‘username’ column?

yeah if that is your CSV you have to adjust your code to

$_.members

Also possibly it can’t find it and you might have to query AD first.

1 Spice up

this is my csv file. i made a slight change in the .csf file. now im getting a message that says

add-ADGroupMember : Cannot find an object with identity: ‘Frank Mendoza’ under: ‘DC=nlacrc,DC=org’.
At line:1 char:54

  • … ach-Object {add-ADGroupMember -Identity $.groupname -Members $.user …
  • CategoryInfo : ObjectNotFound: (Frank Mendoza:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
  • FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

testgroup.txt (50 Bytes)

Neally:
Also possibly it can’t find it and you might have to query AD first.

this is exactly what is happening, you have to either use the samaccountname, or query AD first.

the users are in another group called VDI_production_FI. so how do i point the location of the users which are currently in vdi_production_fi and have specified users transferred from that group to vdi_production. if there is another way i can do it without using the .csv file please advise. i greatly appreciate your assistance.

You could do something like this:

Get-ADGroupMember -Identity VDI_Production_FI | ForEach { Add-ADGroupMember -Identity VDI_Production -Members $_; Remove-ADGroupMember -Identity VDI_Production_FI -Members $_ }

This will find the users in the VDI_Production_FI, add them to VID_Production, and then remove them from VDI_Production_FI.

1 Spice up

Neally I tried running this command to quarry AD but im running to some dificulties.

PS P:\> Get-ADUser -Filter * -SearchBase "OU=vdi_production_fi,ou=useraccounts,DV=nlacrc,DC=org"

Get-ADUser : The supplied distinguishedName must belong to one of the following partition(s): 'DC=nlacrc,DC=org , CN=Configuration,DC=nlacrc,DC=org , 
CN=Schema,CN=Configuration,DC=nlacrc,DC=org , DC=ForestDnsZones,DC=nlacrc,DC=org , DC=DomainDnsZones,DC=nlacrc,DC=org'.
At line:1 char:1
+ Get-ADUser -Filter * -SearchBase "OU=vdi_production_fi,ou=useraccount ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 

will i be able to use a .csv file with the code that you gave me to move the users? the system admin wants me to move the users in small batches. he will be sending me the names of the users that are okay to move.

Is VDI_Production_FI a group or an organizational unit?

VDI_production_fi is a security group in AD

No, the code that I posted does not use CSV, because you requested a method that does not require a CSV file.

Neally i appreciate your patience.

is there a way i can specify the names of the users that i’m assigned to move. kinda like “please move user " username” from vdi_production_fi to vdi_production_ then remove username from vdi_production_fi"

If you need to use a CSV file, you should follow the advice that Neally gave you.

Yes.

$User = Get-ADUser <username>
Add-ADGroupMember -Identity VDI_Production -Members $User
Remove-ADGroupMember -Identity VDI_Production_FI -Members $User

so I only need to enter the username in the first line correct?

Correct.

$User = Get-ADUser <rick james>
Add-ADGroupMember -Identity VDI_Production -Members $User
Remove-ADGroupMember -Identity VDI_Production_FI -Members $User

Like so?

Not quite.

  1. I used angle bracket <> to represent information that you need to enter, because Powershell documentation follows that formatting convention. The angle brackets are not part of the syntax.

  2. You need to use samAccountName, objectGUID, or distinguishedName for Get-ADUser. It does not accept displayName, because that is not a unique way to identify an account.

$User = Get-ADUser samAccountName
Add-ADGroupMember -Identity VDI_Production -Members $User
Remove-ADGroupMember -Identity VDI_Production_FI -Members $User

will it work if i enter multiple user names in line one?. if so will it work if i separate the multiple names by quotation marks " ".