Use Get-groupmember to filter get-aduser

lee roberts 186 Reputation points
2021-03-17T19:42:18.507+00:00

Hi, I'm new to PowerShell and was wondering if there is a way of using the results i get from Get-groupmember to filter my results for get-aduser. What I'm trying to achieve, I have 4groups: GroupA, GroupB, GroupC, GroupD. From the members in these groups I would like to get the following information: Firstname Surname UPN Email Address Account Enabled Last Logon date Date Account created Using the below a can get a list of the users in these groups: Get-ADGroupMember -Identity "GroupA" |%{get-aduser $.SamAccountName | select Name} And adding: $groupmember=Get-ADGroupMember -Identity "GroupA" |%{get-aduser $.SamAccountName | select Name} I can get this to a variable but i can't work out how to use this with Get-aduser for the information a want. Please help!!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-03-18T04:37:41.053+00:00

    Hi,

    The EmailAddress, LastLogonDate and Created are extended properties retrieved by the Get-ADUser cmdlet. If you want to retrieve these properties you can use the Properties parameter to specify them.

    Get-ADGroupMember -Identity $group | ForEach-Object {  
            Get-ADUser -Identity $_.SamAccountName -Properties EmailAddress, LastLogonDate, Created |   
            Select-Object -Property Name, GivenName, Surname, UserPrincipalName, EmailAddress, Enabled, LastLogonDate, Created   
        }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-03-17T20:07:22.413+00:00

    What information do you want? You're only getting the user's name into the $groupmember array. If you remove the "Select name" you'll have an array of User objects in the $groupmember array.


  2. JC 1 Reputation point
    2022-08-24T18:46:07.357+00:00

    Can I add the group name to a column in output?

    Get-ADGroupMember -Identity $groups | ForEach-Object {
    Get-ADUser -Identity $_.SamAccountName -Properties EmailAddress, LastLogonDate, Created |
    Select-Object -Property Name, GivenName, Surname, UserPrincipalName, EmailAddress, Enabled, LastLogonDate, Created
    } | Format-Table -AutoSize

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.