Good Morning Everyone, I need some help.<\/p>\n
I am learning powershell and still working on understanding it, but I have a lot of users that I need to update information for the user as well as add them to their specific groups. I would like to be able to use a .csv file to do all this with powershell. Currently I am doing it line by line.<\/p>\n
here is what I have so far:<\/p>\n
Set-Aduser Usernamehere -Description “000 - SomeJob”
\nSet-Aduser usernamehere -Office “Somewhere”
\nSet-Aduser usernamehere -title “SomeJob”
\nSet-Aduser usernamehere -Department “UM Shelter Program - 000000”
\nSet-Aduser usernamehere -Company “*********************”<\/p>\n
This above code works nice and does what it is supposed to do. My issue is the next piece.<\/p>\n
Get-ADUser -SearchBase ‘OU=New Hires,OU=*** Users,DC=***,DC=org’ -Filter * | % {Add-ADGroupMember -Identity 000Group -Members usernamehere}<\/p>\n
When I run this line to put users into their groups, it does add them, but then I get an error saying they are already in that group. see next line.<\/p>\n
Add-ADGroupMember : The specified account name is already a member of the group
\nAt line:1 char:83<\/p>\n
<\/code><\/pre>\n<\/li>\n- CategoryInfo : NotSpecified: (000Group:ADGroup) [Add-ADGroupMember], ADException<\/li>\n
- FullyQualifiedErrorId : The specified account name is already a member of the group,Microso
\nft.ActiveDirectory.Management.Commands.AddADGroupMember<\/li>\n<\/ul>\nNow this error will come up several times before it will goto the next line for adding the user to another group.<\/p>\n
Is my line incorrect for adding a user to a group? How can I get this to just run without the error running a few times before it moves to the next?<\/p>\n
I have 300 users to update and running each line takes time, and want to be able to use a .csv to add all the information and Groups but I don’t know how. I could use some assistance while I am learning. So please be patient.<\/p>\n
Thank you for any help you can provide.<\/p>\n
G<\/p>","upvoteCount":3,"answerCount":18,"datePublished":"2018-01-22T10:35:23.000Z","author":{"@type":"Person","name":"gpritchett","url":"https://community.spiceworks.com/u/gpritchett"},"suggestedAnswer":[{"@type":"Answer","text":"
Good Morning Everyone, I need some help.<\/p>\n
I am learning powershell and still working on understanding it, but I have a lot of users that I need to update information for the user as well as add them to their specific groups. I would like to be able to use a .csv file to do all this with powershell. Currently I am doing it line by line.<\/p>\n
here is what I have so far:<\/p>\n
Set-Aduser Usernamehere -Description “000 - SomeJob”
\nSet-Aduser usernamehere -Office “Somewhere”
\nSet-Aduser usernamehere -title “SomeJob”
\nSet-Aduser usernamehere -Department “UM Shelter Program - 000000”
\nSet-Aduser usernamehere -Company “*********************”<\/p>\n
This above code works nice and does what it is supposed to do. My issue is the next piece.<\/p>\n
Get-ADUser -SearchBase ‘OU=New Hires,OU=*** Users,DC=***,DC=org’ -Filter * | % {Add-ADGroupMember -Identity 000Group -Members usernamehere}<\/p>\n
When I run this line to put users into their groups, it does add them, but then I get an error saying they are already in that group. see next line.<\/p>\n
Add-ADGroupMember : The specified account name is already a member of the group
\nAt line:1 char:83<\/p>\n
\n- … -Filter * | % {Add-ADGroupMember -Identity 000Group -Members usernamehere}<\/li>\n
- \n
<\/code><\/pre>\n<\/li>\n- CategoryInfo : NotSpecified: (000Group:ADGroup) [Add-ADGroupMember], ADException<\/li>\n
- FullyQualifiedErrorId : The specified account name is already a member of the group,Microso
\nft.ActiveDirectory.Management.Commands.AddADGroupMember<\/li>\n<\/ul>\nNow this error will come up several times before it will goto the next line for adding the user to another group.<\/p>\n
Is my line incorrect for adding a user to a group? How can I get this to just run without the error running a few times before it moves to the next?<\/p>\n
I have 300 users to update and running each line takes time, and want to be able to use a .csv to add all the information and Groups but I don’t know how. I could use some assistance while I am learning. So please be patient.<\/p>\n
Thank you for any help you can provide.<\/p>\n
G<\/p>","upvoteCount":3,"datePublished":"2018-01-22T10:35:23.000Z","url":"https://community.spiceworks.com/t/help-with-powershell-active-directory-issue-learning/629954/1","author":{"@type":"Person","name":"gpritchett","url":"https://community.spiceworks.com/u/gpritchett"}},{"@type":"Answer","text":"
I
\nIf you already have the username why bother with the Get-ADUser part at all?<\/p>\n
This should be enough:<\/p>\n
Add-ADGroupMember -Identity 000Group -Members usernamehere\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-01-22T10:51:36.000Z","url":"https://community.spiceworks.com/t/help-with-powershell-active-directory-issue-learning/629954/2","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},{"@type":"Answer","text":"You can update the properties in 1 go like this:<\/p>\n
Set-Aduser Usernamehere -Description \"000 - SomeJob\" -Office \"Somewhere\" -title \"SomeJob\" -Department \"UM Shelter Program - 000000\" -Company \"*********************\"\nAdd-ADGroupMember -Identity 000Group -Members usernamehere\n\n<\/code><\/pre>\nsaves multiple calls to Set-ADUser.<\/p>\n
Though using a technique called splatting makes it nicer to read:<\/p>\n
$userParams = @{\n Identity = Usernamehere\n Description = \"000 - SomeJob\"\n Office = \"Somewhere\"\n title = \"SomeJob\"\n Department = \"UM Shelter Program - 000000\"\n Company = \"*********************\"\n}\nSet-Aduser @userParams\nAdd-ADGroupMember -Identity 000Group -Members usernamehere\n\n<\/code><\/pre>\nnote how it changes from $userParams when creating the hash table to @userParams<\/span> for the splatting…<\/p>","upvoteCount":1,"datePublished":"2018-01-22T10:56:54.000Z","url":"https://community.spiceworks.com/t/help-with-powershell-active-directory-issue-learning/629954/3","author":{"@type":"Person","name":"psophos","url":"https://community.spiceworks.com/u/psophos"}},{"@type":"Answer","text":"Hello M Boyle,<\/p>\n
Thank you for the help. I am still learning and when you look online there is to much confusion. I thought that using Get-ADuser was needed so you could set the information for the user.<\/p>\n
I am starting to see what you have done, and this is very helpful indeed. You are AWESOME!!!<\/p>\n
So the next part of my learning is being able to put all 300 users into a .csv file and then be able to import that and Set User information and also add the user to multiple groups that they belong to?<\/p>\n
Does this make since?<\/p>\n
G<\/p>","upvoteCount":0,"datePublished":"2018-01-22T11:29:39.000Z","url":"https://community.spiceworks.com/t/help-with-powershell-active-directory-issue-learning/629954/4","author":{"@type":"Person","name":"gpritchett","url":"https://community.spiceworks.com/u/gpritchett"}},{"@type":"Answer","text":"