Link to home
Start Free TrialLog in
Avatar of waltforbes
waltforbesFlag for Bahamas

asked on

I Need to Get Group and User Info from AD

Points of My Scenario:
1. I am admin of a Windows Server 2008 R2 domain.
2. I need a powershell script that will create the following CSV or XLS output: (a) Retrieve all AD groups, then (b) List members for each group, next (c) provide last logon date/time, enabled/disabled status, and creation date for each member of the group.
3. The output will be used to create the following style XLS report attached.

SUMMARY: Please provide a powershell script that can provide the information in the attached spreadsheet (also explained in points 1 thru 3).
User-Review-Template.xlsx
Avatar of Pavel Nagaev
Pavel Nagaev
Flag of Russian Federation image

Try this one.

import-module activedirectory



Get-ADGroup -filter * | Sort -Property Name |%{

"Group:$($_.Name)"

  Get-ADGroupMember $_ | sort -Property objectClass,Name  |%{
 
    if ($_.objectClass -eq "group"){
     "           +------- subgroup   $($_.name )"

    }else
    {

   $user= Get-ADUser -identity $_.distinguishedName -Properties DisplayName, Enabled,LastLogonDate,whenCreated 

    "                  $($User.samaccountname,  $User.DisplayName, $User.Enabled, $User.LastLogonDate, $User.whenCreated )"
  
    }

  }
}

Open in new window

Avatar of waltforbes

ASKER

Hi pgnev:
this is an awesome script - wow! However, I noted 2 errors:
1. The Domain Controllers group & the Domain Computers group listed my domain account for each computer account in the respective groups.
2. Other computer groups did similarly - i.e., they listed a service account instead of the computers - for each computer in the group.

Question: Why does this happen? Is there a fix?
I modified script. Please replace "d:\temp1.csv" to correct path.

You will get what you wanted.

This script isn't ideal but it works.

import-module activedirectory

Get-ADGroup -filter * | Sort -Property Name |%{

$mGroup=$_.Name

Get-ADGroupMember $_ | sort -Property objectClass,Name  |%{
 
 $Mpar=$_

switch ($Mpar.objectClass)
{
    'group' {
    }
     
    'user' {
                    $user= Get-ADUser -identity $Mpar.distinguishedName -Properties DisplayName, Enabled,LastLogonDate,whenCreated -ErrorAction SilentlyContinue

"$mGroup$($User.samaccountname)`t$($User.DisplayName)`t$($User.Enabled)`t$($User.LastLogonDate)`t$($User.whenCreated )`t" >>d:\temp1.csv

    }
   'computer'{
    }
    Default {}
}

  }
}

Open in new window

To pnagaev: The Group name and username are concatenated. How to correct this?
ASKER CERTIFIED SOLUTION
Avatar of Pavel Nagaev
Pavel Nagaev
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bravo! Bravo! You did it, Pnagaev! Many thanks!
you are welcome!