Can a computer be added to an AD group during creation with PowerShell?

0
1
Asked By MellowCedar42 On

I'm building a PowerShell script that creates computer accounts and adds them to a specific Active Directory group. Creating the computer works with New-ADComputer, but Add-ADGroupMember fails with an access-denied error when run afterward.

In the Active Directory Users and Computers GUI, I can add the computer to the group during the creation workflow, even though my account apparently cannot modify that group after the computer object exists. I'm trying to understand whether the GUI is performing these operations as one transaction or using a different permission model.

I've tried creating the computer and then piping the result to Add-ADGroupMember with -PassThru, but the computer still is not added. Is there a supported way to reproduce the GUI behavior in PowerShell without granting my account permission to modify the group?

4 Answers

Answered By RookAndPine5 On

The important question is what the GUI is actually allowed to do. If the same account can add the computer through the creation dialog, the delegated permission may be tied to the computer-creation workflow or to a particular OU, rather than being general permission to write the group’s member attribute. PowerShell cannot generally fake that workflow by creating the object halfway and then modifying the group.

If the failure is intermittent rather than consistently access denied, use the same -Server value for both commands and verify the object exists on that domain controller before adding it. If it is consistently permission denied, the proper fix is delegated rights or a service account designed for this task.

Answered By BlueNoodle18 On

Use -PassThru and pass the resulting computer object explicitly. Also specify the same domain controller for every operation so you do not run into replication or object lookup differences:

$dc = 'DC01.example.com'
$ou = 'OU=Computers,DC=example,DC=com'
$computer = New-ADComputer -Name $ComputerName -Path $ou -Server $dc -PassThru
Add-ADGroupMember -Identity $GroupName -Members $computer -Server $dc

This is cleaner than piping into a command that also has an explicit -Members value. However, it will only fix object resolution or replication issues; it cannot bypass missing rights on the group.

Answered By QuartzHarbor7 On

The Active Directory GUI can create the computer and assign its group memberships as part of the same creation operation, but PowerShell exposes those as separate commands. New-ADComputer must create the object before Add-ADGroupMember can target it, and the second operation still requires permission to modify the group. Piping the commands together does not change that permission check.

A useful alternative is to create a disabled template computer with the desired properties and memberships, then use New-ADComputer -Instance to create new accounts from that template. Make sure the template object includes the properties you want copied, including its group-membership information where applicable. This is a workaround worth testing, but it does not magically grant permission if the directory refuses the resulting membership change.

Answered By CobaltMango63 On

The GUI and PowerShell are not necessarily making identical directory calls. New-ADComputer and Add-ADGroupMember are separate operations, so creating the account first is expected. A delay or forced replication may help when the error is caused by different domain controllers seeing different object state, but waiting will not resolve an authorization error.

Also verify that -Path contains the OU distinguished name itself, such as OU=Computers,DC=example,DC=com, rather than accidentally incorporating the computer name into the path.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.