Need help with modifying the existing script

I have the script which gives output as mentioned below, I need help in modifying the output and adding an attribute to the script

Existing Output
Name Category Scope Members
Domain Admins Security Global Administrator,user1,user2
Expected Output
Name Category Scope Members UserID
Domain Admins Security Global Administrator admin
Domain Admins Security Global user1 usr1
Domain Admins Security Global user2 usr2


#// Start of script
#// Get year and month for csv export file
$DateTime = Get-Date -f "yyyy-MM-DD"

#// Set CSV file name
$CSVFile = "C:\AD_Groups"+$DateTime+".csv"

#// Create emy array for CSV data
$CSVOutput = @()

#// Get all AD groups in the domain
$ADGroups = Get-ADGroup -Filter *

#// Set progress bar variables
$tot = $ADGroups.count

foreach ($ADGroup in $ADGroups) {
    #// Set up progress bar
    $status = "{0:N0}" -f ($i / $tot * 100)
    Write-Progress -Activity "Exporting AD Groups" -status "Processing Group $i of $tot : $status% Completed" -PercentComplete ($i / $tot * 100)

    #// Ensure Members variable is empty
    $Members = ""

    #// Get group members which are also groups and add to string
    $MembersArr = Get-ADGroup -filter {Name -eq $ADGroup.Name} | Get-ADGroupMember | select Name
    if ($MembersArr) {
        foreach ($Member in $MembersArr) {
            $Members = $Members + "," + $Member.Name
        $Members = $Members.Substring(1,($Members.Length) -1)

    #// Set up hash table and add values
    $HashTab = $NULL
    $HashTab = [ordered]@{
        "Name" = $ADGroup.Name
        "Category" = $ADGroup.GroupCategory
        "Scope" = $ADGroup.GroupScope
        "Members" = $Members

    #// Add hash table to CSV data array
    $CSVOutput += New-Object PSObject -Property $HashTab

#// Export to CSV files
$CSVOutput | Sort-Object Name | Export-Csv $CSVFile -NoTypeInformation

#// End of script

Parents Reply Children
No Data