r/PowerShell • u/Flubbip • May 12 '26
Question New-ADcomputer and Add-ADGroupMember: Adding an object to a group mid-creation
Trying to make a simple script to add and remove lists of computers. Creating the computer isn't an issue, it's the adding the computer to a group that is.
When manually adding a computer to AD, while still in the creation GUI, we add it to a group that allows any admin account to add it to a domain, not just the admin that created it. When you try adding it to that group once the object is already created, it will give you an error mentioning that you don't have permissions to modify that group you selected.
Now, I don't have access to the exact code since that's at work, but it's pretty simple as I am just trying to get a working concept before I make it into a proper script, therefore I will summarize the code.
New-ADComputer -Name "$computerName" -Path "$computerName + OU path"This works just fine. It makes the computer with the right name, in the right place.
New-ADComputer -Name "$computerName" -Path "$computerName + OU path"
Add-ADGroupMember -Identity "groupName" -Members "$computerName"This makes the computer but doesn't add it to the group, since if anyone tries to add it after the creation, permission denied.
New-ADComputer -Name "$computerName" -Path "$computerName + OU path" |
Add-ADGroupMember -Identity "groupName" -Members "$computerName"This does not return an error, but when checking the computer, it isn't in the group.
I also tried -passThru on the New-ADComputer side but gives me a pipeline error.
(also tried a few other things but again, don't have access to it at home, but if needed I can get it)
From what I am seeing, when using the AD module, there isn't a way to add a computer to a group while creating the computer, since creation and adding are different cmndlets. The creation will ALWAYS have to run before adding, not during, otherwise there isn't a target for the Add-ADGroupMember, meaning it would return object doesn't exist.
My question is, is this 100% right? Is there no way to mimic the process of adding the computer to a group while creating said computer, in order to not require permissions for that group? Yes, I could try to acquire the rights for the group in question, but that might honestly be the most difficult course of action. And no, going around the permissions during the manual creation isn't "bad". It's built that way for some stupid reason and I don't have the pay to be able to change that.
1
u/raip May 12 '26
Why do you think this would work without having permissions to modify the group to begin with?
3
u/purplemonkeymad May 12 '26
You can create computers from template accounts the same as with users. If you create a dummy computer account (set to disabled) with the required properties and memberships, then you can use -Instance to pass a computer object to New-AdComputer and it will use it as a template for the new computer account. See example 3.
3
u/psdarwin May 12 '26
Seems like a GUI vs PowerShell issue - remember that the AD GUI tools are not backed up by PS, so the same rules may not apply. If only they were...
In PowerShell this process is two distinct steps, but not in the GUI.
In this case, the GUI creates the computer object with the group membership already set. You start the new computer object creation process in ADUC and set all the properties of the computer and group membership in that GUI windows, and then click OK. The clicking OK does all the things at once.
1
u/MostFckingWanted May 12 '26
i dont really understand this part.. how can you add the computer in the group (manually) when you have no rights to edit the group? can you edit the group in generell?
Yes.
Not much of a difference but maby try this one..
or with credentials..
1
u/locked_ring May 12 '26
What is the exact error when adding it to the group?
Try specifying the AD domain controller in the -server switch value if you have multiple DCs.
1
u/BlackV May 12 '26 edited May 12 '26
Personally I recommend use your objects and use the
-serverparameter to ensure ALL actions are happening on the same DCalso your "path" implies you have a separate OU for each computer?
I'd do something like
Advantage is, you are validating you have real objects as you go
but you could skip the group/ou calls and just use the strings
Fix up copy/paste/eyeball errors
1
u/Godcry55 May 17 '26
You need permissions…if you can do it via GUI then you need a service account that has adequate permissions to perform the same action.
PS is simply automating GUI click ops.
1
u/UserProv_Minotaur May 12 '26
You might need to add in a wait timer between AD object creation and attempting to add it to the group, unless you can add multiple MemberOf values using -OtherAttributes - which is not something I've tried myself, as my organization has the AD Computer object created when the PC is joined to the domain.