Answered by:
Add-Group Members to diffrents groups Using a .CSV file

Question
-
Good evening guys, so i already migrate the groups from domainA to domainB, just the groups since users are new in domainB and they cant be migrated, now i need to fix the membership on this grups but somehow is not working.
this is the script i have:
$Members = Get-Content -path 'C:\export\Csvfilename.csv'`
-header GroupName,Members
foreach ($member in $members)
{Add-Groupmember -id $member.GroupName -members $member.members}
#NOte, $Member.Members is coming out with more than one user, and i dont know how to make this go thru, if i type user1,user2,user3 they get added to the group but if i put these three users in one variable they dont#
Please help.
Thanks.
Answers
-
If you have all the member names in one column ("Members") of a single row (for each group), how are they separated?
If you've used (for example) a semicolon to separate the member names you can split the contents of the "Members" column into an array and use the array as the value for the '-Members" parameter.
Something like this:
Add-Groupmember -id $member.GroupName -members ($member.members -split ';')
. . . but that's a big assumption on my part.
How about posting a few examples from your CSV that demonstrates the problem?
As JRV pointed out, if you're using a CSV formatted file, then Get-Content isn't the right way to handle it. Use Import-CSV instead.
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Edited by Rich Matheisen [Ex-MVP (retired)] Thursday, November 14, 2019 3:52 AM
- Marked as answer by Junior Gómez Thursday, November 14, 2019 10:01 PM
- Unmarked as answer by Junior Gómez Thursday, November 14, 2019 10:02 PM
- Marked as answer by Junior Gómez Thursday, November 14, 2019 10:02 PM
All replies
-
-
-
What is "the first lane"? Is it a type of file? A Csv file is a very special type of file and must have a very specific structure.
It sounds like you have a file that has duplicates. I see nowhere were the is a "User" in the file code.
Do you mean that the file has duplicate rows?
Header should not be on a separate line. You should not use "header" if the file has headers. You cannot import a Csv using Get-Content.
Please read this to understand what a Csv is and how it is structured: https://en.wikipedia.org/wiki/Comma-separated_values
To learn how to work with Csv files search for the following: "powershell csv file"
\_(ツ)_/
-
If you have all the member names in one column ("Members") of a single row (for each group), how are they separated?
If you've used (for example) a semicolon to separate the member names you can split the contents of the "Members" column into an array and use the array as the value for the '-Members" parameter.
Something like this:
Add-Groupmember -id $member.GroupName -members ($member.members -split ';')
. . . but that's a big assumption on my part.
How about posting a few examples from your CSV that demonstrates the problem?
As JRV pointed out, if you're using a CSV formatted file, then Get-Content isn't the right way to handle it. Use Import-CSV instead.
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Edited by Rich Matheisen [Ex-MVP (retired)] Thursday, November 14, 2019 3:52 AM
- Marked as answer by Junior Gómez Thursday, November 14, 2019 10:01 PM
- Unmarked as answer by Junior Gómez Thursday, November 14, 2019 10:02 PM
- Marked as answer by Junior Gómez Thursday, November 14, 2019 10:02 PM
-
I would also note that you could never have tested the code you posted as the command "Get-Content" has no parameter named "Header". This means that you either posted the wrong code or you have some other issue that you haven't told use.
When in doubt always use the help system to validate the usage of you code before asking in a forum.
help get-content -online
help Add-GroupMember -fullPowerShell is designed as a self-help system which is why it has a number of commands intended to help users understand the commands and the objects. Unfortunately most new techs refuse to actually attempt to learn PowerShell so they never learn this.
\_(ツ)_/
- Edited by jrvModerator Thursday, November 14, 2019 4:12 AM
-
I would suggest that you are trying to use the AD CmdLet to add members. If that is the case then this is the correct command.
\_(ツ)_/
-
This is the script i used to generate my CSV file:
https://gallery.technet.microsoft.com/scriptcenter/Export-all-AD-groups-and-3ae6fb42
it worked good, 4 colums, GROUPname, Category, Scope, Member
Group1| Security | Global | Jose,James,Jeff
Im looking for a script that gets Jose,James,jeff put it into one variable because there more users and groups.
thank your for your fast responses and sorry im new on Powershell.
-
Did you read the links and search for the answers to the query I pos5ted above.
It also looks like you need to specify a correct delimiter.
You must put some effort into understanding the help given. We do not really know what is in you file because all of your poste\s just give a little bit of the info and each time we find out that what you have posted before is not correct. Much of this has to do with your not using correct terms and making assumptions that are incorrect.
You clearly have no experience with scripting or coding of any kind which is making this very difficult for you. You should start by learning basic PowerShell. That will help you understand the correct terms and will help you to ask an unambiguous question.
Also know that you can easily look up any technical terms you do not understand using a web search site.
start by searching for "powershell csv files"
\_(ツ)_/
-
thanks you all for your responses, i do really apreaciate them, JRV thank you, i will take a look of those links, sorry is just that i am working in this project right now so I havent got the time to get better documented on Powershell, still i appreaciate your time and your suggestions i do really need to get better on Powershell Scripting, Hey Rich even though my script worked with your response now im facing the issue that if one of the user that i have listed for a particular group is not in the current AD the rest of the users that are or may be in it are not getting added. any thoughts?
Regards.
Junior Gomez.
-
-
How about posting the code you're using now? We still don't know what your CSV looks like.
If you need to verify that every user exists in the AD then you'll have to do something like "Get-ADUser" and add only the user names that exist to an array. Then use that array in the Add-ADGroupMember instead of the raw, un-validated, contents of the CSV.
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
-
Import-Csv -path 'C:\Export\T1adgroupmember.csv'| ForEach-Object {Add-ADGroupMember -Identity $_.GroupName -Members ($_.Members -split ';')-ErrorAction SilentlyContinue}
My csv Looks Like this:
GroupName, Members IPO-GroupName1, Jose;Jeff,Jhon
IPO-GroupName2,Steve;Junior,Katherine
IPO-GroupName3,Cesar;Thom;Keyri
- Edited by Junior Gómez Friday, November 15, 2019 2:01 PM
-
The command will stop on the first error even if you add error output suppression. TO avoid this you either need to check the membership or you need to use Try/Catch.
If the users are in a different domain then you cannot use the SamAccountName. You must use the full DN and you must have write permission on the user object in the remote domain.
\_(ツ)_/
-
Import-Csv -path 'C:\Export\T1adgroupmember.csv'| ForEach-Object {Add-ADGroupMember -Identity $_.GroupName -Members ($_.Members -split ';')-ErrorAction SilentlyContinue}
My csv Looks Like this:
GroupName, Members IPO-GroupName1, Jose;Jeff,Jhon
IPO-GroupName2,Steve;Junior,Katherine
IPO-GroupName3,Cesar;Thom;Keyri
\_(ツ)_/
-
-
thank you jrv i added Get-aduser first and now i got it to work, Im fixing the CSV now.
thank you all.
Junior.
Not what I posted. You need to check if the user is already a member of the group.
Get-AdGroupMember group user
If the user has already been added then the code will fail.
\_(ツ)_/
-
If I import that CSV I get this as a result:
GroupName Members --------- ------- IPO-GroupName1 Jose;Jeff IPO-GroupName2 Steve;Junior IPO-GroupName3 Cesar;Thom;Keyri
Notice that if you are using a comma (which is the default) as a delimiter, you have more columns than the header row says there are. So, the first order of business is to get a CSV file that properly constructed! In other words, use that semi-colon as the value separator in the Members column and a comma to separate the GroupName from the Members. Then you can split the Members using the semi-colon.
If you have names in the Members column that have commas in them, then you need to surround (i.e. protect) the data in that column by surrounding the entire set of names in double quotes (e.g. '"Name1;Name,two;Name3"')
That still won't fix your problem of there being account names in the Members list don't exist in the AD you're working with. See JRV's reply for suggested ways of dealing with that.
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)