Asked by:
get-adprincipalgroupmembership

Question
-
When i run this script i want to see GroupName 1st column, second column i would like to see username (entry ok in firstline) all others read Microsoft.ActiveDirectory.Management.ADPropertyValueCollection, I would like to see the firstname.lastname for all groups
***************************
$user = Read-Host -Prompt "Please enter a user name"
import-module ActiveDirectory
$status = (Get-ADPrincipalGroupMembership $user | select name, $user) | export-csv -path c:\temp\gm.csv -NoTypeInformation
$status***************************
name christopher.ellis
Domain_Head Office Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Recipient Management Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Discovery Management Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Server Management Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Domain Users Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
HO_ITSTimeSheets_R Microsoft.ActiveDirectory.Management.ADPropertyValueCollection
Heat_Users_Global Microsoft.ActiveDirectory.Management.ADPropertyValueCollectionThursday, October 3, 2019 2:30 PM
All replies
-
Hi,
this is becasue of the following part:
Get-ADPrincipalGroupMembership $user | select name, $user
You cannot select $user, you need to chose different group properties to be displayed. All the "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" stuff is displayed, because of this. Here an example of chosing different group properties (remember, you are getting all groups the user is member of, so you display their properties):
$user = Read-Host -Prompt "Please enter a user name" import-module ActiveDirectory $status = (Get-ADPrincipalGroupMembership $user | select name, SamAccountName,SID) | export-csv -path c:\temp\gm.csv -NoTypeInformation $status
So the ouput will show you all groups the user is memer of with their names, SamAccountNames and SIDs. You can of course chose whatever properties you want.
Hope this helps. Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Edited by Stoyan ChalakovMVP Thursday, October 3, 2019 2:44 PM
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Tuesday, October 8, 2019 8:36 AM
Thursday, October 3, 2019 2:40 PM -
You have to get the SamAccountName:
Get-ADPrincipalGroupMembership $user | Select SamAccountName,@{n='User';e={$user}}
\_(ツ)_/
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Tuesday, October 8, 2019 8:36 AM
Thursday, October 3, 2019 2:41 PM -
does not work as expected samaccountname produces the samaccountname of the group not the user belonging to group
$status = (Get-ADPrincipalGroupMembership $user | select name, SamAccountName,SID)
Thursday, October 3, 2019 3:34 PM -
it worked thank youThursday, October 3, 2019 3:42 PM
-
does not work as expected samaccountname produces the samaccountname of the group not the user belonging to group
$status = (Get-ADPrincipalGroupMembership $user | select name, SamAccountName,SID)
The "User" field is the user that belongs to the group. You need to think of and understand what these things mean. "Name" is the user and "SamAccountName" is the group name.
If you want it to be more explicit then:
Get-ADPrincipalGroupMembership $user | Select @{n='GroupName';e={$_.SamAccountName}},@{n='GroupUser';e={$user}}
\_(ツ)_/
Thursday, October 3, 2019 3:55 PM -
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Just do it.
Tuesday, October 8, 2019 8:36 AM