Script to find all users created in last 7 days including AD group memebership and output csv

MOHAMMED SIDDIQUI 21 Reputation points
2022-06-13T08:44:55.953+00:00

From the below script issued by (JRV) I'm getting the list of all ad groups the user is part of. However when I try to modify the same and filter for only security groups, not able to do so.
Would like your help in getting the username, Whencreated, Enabled, GroupName, GroupCategory.

Also anyway the output can be changed to HTML or CSV?

Get-ADUser -Filter * -Properties * |
Where-Object { $.whenCreated -ge $date } |
ForEach-Object{
$user = $

$.memberOf |%{
$g = Get-ADGroup $

Add-Member -InputObject $user -MemberType NoteProperty -Name GroupName -Value $g.Name -PassThru -Force
}
} |
Format-Table -GroupBy Name -Property GroupName, whenCreated,DisplayName,mail,ipphone,Manager

Any Help is much appreciated.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
3,613 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,621 questions
{count} votes

Accepted answer
  1. Newbie Jones 1,171 Reputation points
    2022-06-13T11:52:39.447+00:00

    Try the following. This will be one line per group with displayName and whenCreated attributes for the user repeated for each group.
    Please get into the habit of only bringing back the properties you need. (Instead of just using *, which will be every property for every user, which affects the performance of your script).

    $Users = Get-ADuser -Filter * -properties memberof, whenCreated, DisplayName  
      
    $results=@()  
      
    ForEach ($User in $Users) {  
          
        $groups = $User.memberOf | Get-ADGroup | Where-Object {$_.GroupCategory -eq 'Security'}  
        ForEach ($group in $groups) {  
            $props = [ordered]@{  
            displayName=$user.displayName  
            whenCreated=$user.whenCreated  
            Group=$group.Name  
            GroupCategory=$group.GroupCategory}  
      
            $results +=  New-Object -TypeName PSObject -property $props  
        }  
      
    }  
      
    $results | Export-CSV filename.csv -noTypeInformation  
    

0 additional answers

Sort by: Most helpful