0

i want to export csv file with columns "ParentGroupName", "MemberName", "DisplayName"

At the moment it is exporting the three datas into one column.

function getGroups{

    $Groups += Get-ADGroup -Filter * -SearchBase "ou=Groups,ou=DCM,ou=NTG,dc=prod,dc=main,dc=ntgov" | Select-Object -ExpandProperty samaccountname

    return $Groups
}

Measure-Command{ 


$Groups = getGroups
write-host "Groups:" $Groups.Count 

}

Measure-Command{



$date = $(get-date).ToString("dd MMM yyyy")
$global:FileName = "Active Directory Group Membership - DCM -" + $date

$stringBuilder = New-Object System.Text.StringBuilder

foreach ($GroupName in $Groups){

        Get-ADGroupMember -Identity $GroupName | Sort-Object $_.SamAccountName | ForEach-Object {
            $ParentGroupName = (Get-ADGroup -Identity $GroupName).SamAccountName 
            $MemberName = $_.SamAccountName # Member of the Group.


            if ($_.ObjectClass -eq 'group') {



                $DisplayName = ' - '

            } elseif ($_.ObjectClass -eq 'user') {


                $a = (Get-ADUser -Identity $MemberName -Properties Displayname)
                $DisplayName = $a.DisplayName 

            }



            $null = $stringBuilder.Append("$ParentGroupName, $MemberName, $DisplayName")


        }
    } 


outputArray = $stringBuilder.ToString()


out-file C:\Users\augut\Desktop\$FileName.csv


outputArray | out-file C:\Users\augut\Desktop\$FileName.csv
}

1 Answer 1

1

You're making a headache for yourself by manually constructing the CSV file. This can be simplified considerably by constructing a custom object for each item found with the properties you need recorded, then stuffing those into an array and exporting that array to a CSV file.

function getGroups{
    $Groups += Get-ADGroup -Filter * -SearchBase "ou=Groups,ou=DCM,ou=NTG,dc=prod,dc=main,dc=ntgov" | Select-Object -ExpandProperty samaccountname
    return $Groups
}
$Groups = getGroups
write-host "Groups:" $Groups.Count 

$date = $(get-date).ToString("dd MMM yyyy")
$global:FileName = "Active Directory Group Membership - DCM -" + $date

$results = @();

foreach ($GroupName in $Groups){
        Get-ADGroupMember -Identity $GroupName | Sort-Object $_.SamAccountName | ForEach-Object {
            $ItemProperties = @{
                "ParentGroupName" = (Get-ADGroup -Identity $GroupName).SamAccountName;
                "MemberName" = $_.SamAccountName
            }

            if ($_.ObjectClass -eq 'group') {
                $ItemProperties.Add("DisplayName","-");
            } elseif ($_.ObjectClass -eq 'user') {
                $ItemProperties.Add("DisplayName",(Get-ADUser -Identity $MemberName -Properties DisplayName).DisplayName);
            }
            $MyItem = New-Object -TypeName psobject -property $ItemProperties;
            $Results += $MyItem;
            $ItemProperties = $null;
        }
    } 
$results | export-csv -path "C:\Users\augut\Desktop\$FileName.csv" -NoTypeInformation
Sign up to request clarification or add additional context in comments.

3 Comments

thanks it mostly works accept for the displayName which seems to repeat one name for every groupName and userName: for example: mjms Jayne Fairnington DCM_NTH_FOI TP6 Jayne Fairnington DCM_NTH_FOI yfair Jayne Fairnington DCM_NTH_FOI ktimi Jayne Fairnington zzMinisterVowles_O bxl Jayne Fairnington DCM_DNH_SPIO_RW FMD Jayne Fairnington DCM_DNH_SPIO_RW MAT Jayne Fairnington DCM_DNH_SPIO_RW aleon Jayne Fairnington DCM OCM Chief of Staffs amart Jayne Fairnington DCM OCM Chief of Staffs bqb Jayne Fairnington DCM OCM Chief of Staffs
@GuzziT I just made a small edit, try the new version.
It didn't change the output in excel :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.