Active directory | Primary Group

rated by 0 users
This post has 5 Replies | 3 Followers

Top 25 Contributor
Posts 57
Top Contributor
Bernard Posted: 08-06-2009 5:34 AM

Is there a way to find the primary Group of a user ?

via Standard ADSI -> memberof i dont see this primary group.

Top 10 Contributor
Posts 248
Microsoft MVP
Top Contributor

If you can use Quest AD cmdlets, this should work:

Get-QADUser <username>  | Select name, @{n="PrimaryGroup";e={(Get-QADGroup "$($_.Sid.AccountDomainSid)-$($_.PrimaryGroupId)").name}}

 

-aleksandar

http://powershellers.blogspot.com

Top 25 Contributor
Posts 57
Top Contributor

Hi Aleksandar,

I Want to avoid using the Quest AD cmdlets as much as possibe. 

i see the primaryGroupID of a user = ID 513 (This means Domain Users)

How can i find a group by ID ... i checked the properties of the group in AD ... but i dont see one
resembeling the this groupID.

Top 10 Contributor
Posts 248
Microsoft MVP
Top Contributor

This solution uses ADSI and the .NET Framework to retrieve the name of the user's primary group:

$rootDN = ([adsi]"").distinguishedName

$user = [adsi]("LDAP://CN=testuser,OU=Test," + $rootDN)

$groupID = $user.primaryGroupID

$arrSID = $user.objectSid.Value

$SID = New-Object System.Security.Principal.SecurityIdentifier ($arrSID,0)

$groupSID = $SID.AccountDomainSid.Value + "-" + $user.primaryGroupID.ToString()

$group = [adsi]("LDAP://<SID=$groupSID>")

$group.name

 

-aleksandar

http://powershellers.blogspot.com

 

Top 25 Contributor
Posts 57
Top Contributor

Dude thanks ...
the construction of DomainGUID + GroupGUID I didn't know.
Learned a lot today !

Not Ranked
Posts 1

How do you do this using the AD Module for PowerShell?  I want to avoid ADSI syntax.

Page 1 of 1 (6 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.