Your PowerGUI Community: Simplify your PowerShell related GUI and script editing needs.


Check Group Members of several Groups

This question is answered
Hello I'd like to check some groups, their members and the last logon times of the users, to check how many licensed I do need for a Software.

This is what I already have

Now I get a List of Groupnames

#Check Groups

Get-QADGroup -Name groupname* | Select-Object Name


Now I get a List of Groupnames
I'd like to export the Users of those groups including their LastLogon Times into a CSV File, how can I do this ?


Verified Answer
  • Get-QADGroup -Name groupname* | Get-QADGroupMember -Type User | Select-Object Name,LastLogon,LastLogonTimeStamp | Export-Csv LastLogon.csv

All Replies
  • $GroupMembers = @()
    $Groups2Check = @("Group1","Group2")

    #This example uses the Active Directory module from Microsoft.

    function Get-ADUserLastLogon([string]$userName)
    {
    # Modified from:
    # http://technet.microsoft.com/en-us/library/dd378867%28WS.10%29.aspx
    #
    $dcs = Get-ADDomainController -Filter {Name -like "*"}
    $time = 0
    foreach($dc in $dcs)
    {
    $hostname = $dc.HostName
    $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon
    if($user.LastLogon -gt $time)
    {
    $time = $user.LastLogon
    }
    }
    $dt = [DateTime]::FromFileTime($time)
    return $dt }


    Function ConvertTo-Object {
    #
    #http://powershell.com/cs/blogs/tips/archive/2008/11/14/converting-hash-tables-to-objects.aspx
    #

    begin { $object = New-Object Object }

    process {

    $_.GetEnumerator() | ForEach-Object { Add-Member -inputObject$object -memberType NoteProperty -name $_.Name -value $_.Value }  

    }

    end { $object }

    }



    Foreach ($CurrentGroup in $Groups2Check)
    {
    $GroupMembers += Get-AdGroupMember $CurrentGroup | select samaccountname
    #This will add all the members of each group to one Array.
    }
    $SAMS = @()
    foreach ($SAM in $GroupMembers)
    {
    $SAMS += $SAM.samaccountname
    }

    # NOTE: This can be done in one Foreach loop, I'm just doing it this way to make it
    # easier to see what is going on... (I hope)

    $LastLogons = @{}
    foreach ($User2Check in $SAMS)
    {
    $LL = Get-ADUserLastLogon $User2Check
    $LastLogons["$User2Check"]="$LL"
    }
    $LastLogons | ConvertTo-Object | Export-Csv .\LastLogon.csv -notypeinformation


    Hope that helps.

    -Alex
    (Caution: Seems like the site adds text to the script in preview, you might need to clean it up)
  • Get-QADGroup -Name groupname* | Get-QADGroupMember -Type User | Select-Object Name,LastLogon,LastLogonTimeStamp | Export-Csv LastLogon.csv

  • *Bows to the master*
  • thank you very much, great !

  • Shay Levy wrote:

    Get-QADGroup -Name groupname* | Get-QADGroupMember -Type User | Select-Object Name,LastLogon,LastLogonTimeStamp | Export-Csv LastLogon.csv


    I have a very similar problem I need to handle and I executed the above script and it did give me the names of all of the members of the hundreds of groups I had to scan thru but I need one additional piece of data in the output I've not been able to figure out.

    How can I get the name of the group each one of those people are a member of? The licencing company charges a fee based on what role they can perform and our group membership names tell us what role they do. Is there a way to dump the group name and then a list of the users within that group in the CSV?

    Thanks in advance
    Pat Mc-
  • Try this. Do you get all users now?

     Get-QADGroup -Name groupname* | Foreach-Object{
         $Group = $_
         $Group | Get-QADGroupMember -Type User -SizeLimit 0 | Select-Object Name,@{n='GroupName';e={$Group.Name}},LastLogon,LastLogonTimeStamp
     } | Export-Csv LastLogon.csv



    Message was edited by: Shay Levy
  • This worked brilliantly!

    Thank you Shay!!

    Pat Mc-