Score:0

AD User and Group Report

rs flag

I just inherited a process in place that combined 2 PowerShell scripts:

  1. Get list of AD User properties.
  2. Get list of User group memberships

I have added the group membership property to User script, but it dumps all the group memberships in one line, the beauty of this group script they are using is it dumps each group and user to each line so the PM can quickly scan the memberships quickly in one column. This feature I can't get to work in the User report, so I have to manually do both and combine in the Excel sheet for AD Auditing purposes.

I've wracked my brain on this and I am fully aware of the original sin of asking forum members to write scripts, but I feel like this should be so simple. I will continue trying to figure this one out but maybe someone out there has this already written. The main feature is the line by line user group membership column.

SCRIPT 1 - GetADUser list report

# Split path
$Path = Split-Path -Parent "C:\scripts\AD\*.*"

# Create variable for the date stamp in log file
$LogDate = Get-Date -f yyyyMMddhhmm

# Define CSV and log file location variables
# They have to be on the same location as the script
$Csvfile = $Path + "\AllADUsers_$logDate.csv"

# Import Active Directory module
Import-Module ActiveDirectory

# Set distinguishedName as searchbase, you can use one OU or multiple OUs
# Or use the root domain like DC=elon,DC=local
$DNs = @(
    "DC=musk,DC=com"
)

# Create empty array
$AllADUsers = @()

# Loop through every DN
foreach ($DN in $DNs) {
    $Users = Get-ADUser -SearchBase $DN -Filter * -Properties * 

    # Add users to array
    $AllADUsers += $Users
}

# Create list
$AllADUsers | Sort-Object Name | Select-Object `
@{Label = "Display name"; Expression = { $_.DisplayName } },
@{Label = "User logon name"; Expression = { $_.SamAccountName } },
@{Label = "Group Name"; Expression = { $_.MemberOf } },
@{Label = "E-mail"; Expression = { $_.Mail } },
@{Label = "Password Last Set"; Expression = { $_.passwordlastset } },
@{Label = "Password never expires"; Expression = { $_.Passwordneverexpires } },
@{Label = "Account status"; Expression = { if (($_.Enabled -eq 'TRUE') ) { 'Enabled' } Else { 'Disabled' } } },
@{Label = "Last logon date"; Expression = { $_.lastlogondate } }|

# Export report to CSV file
Export-Csv -Encoding UTF8 -Path $Csvfile -NoTypeInformation #-Delimiter ";"

Script 2 - Get-ADGroups

$ExportLocation = "c:\scripts\ad\ADAxport.csv"
 
# Get a list of all Active Directory groups
$ADGroupNames = Get-ADGroup -filter * | sort Name | select Name, SamAccountName
$ExportData = @()
 
# Set the current index to 1 (Used for a progress bar)
$currentIndex = 1
 
# Loop through all groups
foreach($GroupName in $ADGroupNames) {
    # Show a nice progress bar
    Write-Progress -Id 0 -Activity "Building report from Active Directory" -Status "$currentIndex of $($ADGroupNames.Count)" -PercentComplete (($currentIndex / $ADGroupNames.Count) * 100)
 
    # Get all membership of a given group and select only the users
    $GroupMembership = Get-ADGroupMember -Identity $GroupName.SamAccountName -Recursive | Where {$_.objectClass -eq "user"} | select distinguishedName
    # Retrieve all of the active users from the list, selecting only the name
    $GroupMembershipUsers = $GroupMembership | ForEach-Object {Get-ADUser -Identity $_.distinguishedName -Properties Enabled} | select Name
 
    # For each member add a new object to the ExportData array
    foreach($User in $GroupMembershipUsers) {
        $ExportData += [PSCustomObject]@{
            User = $User.Name
            Group = $GroupName.Name
            
        }
    }
 
    # Increment the index
    $currentIndex++;
}
 
# Reset the progress bar
Write-Progress -Id 0 -Activity " " -Status " " -Completed
 
$ExportData | Export-Csv -Path $ExportLocation -NoTypeInformation
  
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.