Add-ADGroupMember
Quite an easy one to start with, but quite handy too. Saves a few lines of code from ps1.
Example usage
Define the parameters
Both identity and members take a range of identifiers for the object. These include "Distinguished Name", "GUID", "SID", and "samaccountname". As all the cmdlets have been designed for interoperability, I find it best to use output from other commands like get-aduser.
In a script
Powershell without the Management pack
How you generate the distinguishedname (DN) is up to you, I would use my AD searcher and use the $object.path that is returned.
Quite an easy one to start with, but quite handy too. Saves a few lines of code from ps1.
Example usage
Add-ADGroupMember -identity "Group name" -members "new group member"
Add-ADGroupMember "Group name" "list of new group member"
Define the parameters
Both identity and members take a range of identifiers for the object. These include "Distinguished Name", "GUID", "SID", and "samaccountname". As all the cmdlets have been designed for interoperability, I find it best to use output from other commands like get-aduser.
In a script
#set the group name
$Group = "All Managers"
#get the objects that you want to add to the group (in this case, users with Manager in the description)
$users = get-aduser -filter {description -like "*Manager*"}
#Add the users to the group
Add-ADGroupMember $Group $users
Powershell without the Management pack
How you generate the distinguishedname (DN) is up to you, I would use my AD searcher and use the $object.path that is returned.
$groupDN = "cn=groupname,ou=groups,dc=domain,dc=com"
$UserDN = "cn=username,ou=users,dc=domain,dc=com"
$group = [adsi]("LDAP://" + $groupDN)
$group.Add("LDAP://" + $userDN)
$group.setinfo()
Comments
Post a Comment