0 votes


is there an "easy" way to add a user to all group within his OU or we need to use a script for that ?

by (2.5k points)

In fact it was pretty easy to do that with a really short script :

Import-Module Adaxes
Import-Module ActiveDirectory

#retrieve OU from user DN
$ou = "%distinguishedName%" -replace '^(.*?,OU)', 'OU'

#Add user DN to all group within $ou
$group = Get-ADGroup -SearchBase $ou -Filter * | Add-ADGroupMember -Members "%distinguishedName%"

Some times the script fails with the error directory object not found.

I think this is because my script may attempd to check the new user on a DC which is not replicated yet, and the information about the new utilisateur is missing.

Is it possible to retrieve the name of the DC on which the user are created ? (so i would be able to specify the DC in the script)

1 Answer

0 votes
by (215k points)
selected by
Best answer

Hello Alexandre,

The following script will do the job:

Import-Module Adaxes

$domain = $Context.GetObjectDomain("%distinguishedName%")
$rootDse = $Context.BindToObject("Adaxes://$domain/rootDSE")
$dc = $rootDse.Get("dnsHostName")

Get-AdmGroup -SearchBase "%adm-ParentDN%" -Filter * -Server $dc | Add-AdmGroupMember -Members "%distinguishedName%" -Server $dc

Hi Adaxes,

It seems it is working properly now. Thanks.

Related questions

0 votes
1 answer

Hi Guys, I'm trying to clean all users from Local Group test_group, and next new bunch of users form TXT fiel, by executing the following script as a scheduled task Import-Module ... .txt I have only 600 users. Do you have any idea how to improve this script?

asked Feb 27, 2015 by axmaster (2.7k points)
0 votes
1 answer

Our Help Desk currently 'mirrors' the group membership of a new user based on another existing user in our AD. I'd like to be able to automate this so that the initiator ... and 'paste' it on the new user being created. Any help on this would be appreciated!

asked Apr 21 by RayBilyk (680 points)
0 votes
1 answer

Search - View users from an OU or group Is it possible to configure so that the search only displays users of an OU? Or specific group? https://imgur.com/wl7AIN1

asked Dec 12, 2018 by tiagotoledo (1.7k points)
0 votes
1 answer

Good Afternoon, Currently as far as I can tell when defining a new user we are forced to select a location for a newly created user before defining information like the ... If not, is there a better way of applying this sort of functionality? Regards Josh

asked Apr 21, 2014 by jtop (3.4k points)
0 votes
0 answers

I am trying to find a way to create Groups based off an OU and a list of options (check boxes) within the portal For example: Select the Target OU to add groups ... 3 - Remote Administrators Option 3 - Remote Developers Option 4 - Readers Option 4 - Writers

asked Sep 11 by dknapp (690 points)
2,288 questions
2,043 answers
55,016 users